diff options
author | Nikita Lapkov <nikita.lapkov@mongodb.com> | 2020-08-18 11:37:15 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-09-09 21:10:53 +0000 |
commit | c209bf2d265b59765ee07765654a257abfe76a2a (patch) | |
tree | bbba25045f9c3c9ff73fa1e16a6c0f42a79cb969 | |
parent | 88695a01286b694684bc909f4acc2e6c57ec0ca9 (diff) | |
download | mongo-c209bf2d265b59765ee07765654a257abfe76a2a.tar.gz |
SERVER-26726 Check number of arguments for createIndex, createIndexes and ensureIndex shell commands
(cherry picked from commit be07da4bd6dd394947236857f5510a7ba9c3b0a4)
-rw-r--r-- | jstests/core/create_index_helper_validation.js | 26 | ||||
-rw-r--r-- | jstests/multiVersion/do_upgrade_downgrade.js | 2 | ||||
-rw-r--r-- | jstests/noPassthrough/do_not_rebuild_indexes_before_repair.js | 10 | ||||
-rw-r--r-- | jstests/noPassthrough/rebuild_multiple_indexes_at_startup.js | 10 | ||||
-rw-r--r-- | src/mongo/shell/collection.js | 12 |
5 files changed, 55 insertions, 5 deletions
diff --git a/jstests/core/create_index_helper_validation.js b/jstests/core/create_index_helper_validation.js new file mode 100644 index 00000000000..42aac1912c8 --- /dev/null +++ b/jstests/core/create_index_helper_validation.js @@ -0,0 +1,26 @@ +// Tests that helper functions for creating indexes check the number of arguments passed. + +(function() { + "use strict"; + + const coll = db.create_index_helper_validation; + coll.drop(); + + assert.throws(() => coll.createIndexes( + /* keys */[{a: 1}], + /* options */ {}, + {background: true}, + {unique: true})); + + assert.throws(() => coll.createIndex( + /* keys */ {a: 1}, + /* options */ {}, + {background: true}, + {unique: true})); + + assert.throws(() => coll.ensureIndex( + /* keys */ {a: 1}, + /* options */ {}, + {background: true}, + {unique: true})); +}());
\ No newline at end of file diff --git a/jstests/multiVersion/do_upgrade_downgrade.js b/jstests/multiVersion/do_upgrade_downgrade.js index 02e6162495b..28f1355d98c 100644 --- a/jstests/multiVersion/do_upgrade_downgrade.js +++ b/jstests/multiVersion/do_upgrade_downgrade.js @@ -45,7 +45,7 @@ let foo = testDB.getCollection(c.name); assert.commandWorked(foo.createIndex({id: 1}, {unique: true})); assert.commandWorked( - foo.createIndex({sno: 1}, {name: "sno_1"}, {unique: true, v: 1})); + foo.createIndex({sno: 1}, {name: "sno_1", unique: true, v: 1})); } }); } diff --git a/jstests/noPassthrough/do_not_rebuild_indexes_before_repair.js b/jstests/noPassthrough/do_not_rebuild_indexes_before_repair.js index 12fd52a09f2..3b63c337ebf 100644 --- a/jstests/noPassthrough/do_not_rebuild_indexes_before_repair.js +++ b/jstests/noPassthrough/do_not_rebuild_indexes_before_repair.js @@ -27,8 +27,14 @@ return; } - let coll = rst.getPrimary().getDB(dbName)[collName]; - assert.commandWorked(coll.createIndexes([{a: 1}, {b: 1}], {}, {writeConcern: {w: "majority"}})); + let db = rst.getPrimary().getDB(dbName); + let coll = db[collName]; + + assert.commandWorked(db.runCommand({ + createIndexes: collName, + indexes: [{key: {a: 1}, name: "a"}, {key: {b: 1}, name: "b"}], + writeConcern: {w: "majority"} + })); assert.eq(3, coll.getIndexes().length); rst.awaitReplication(undefined, ReplSetTest.OpTimeType.LAST_DURABLE); diff --git a/jstests/noPassthrough/rebuild_multiple_indexes_at_startup.js b/jstests/noPassthrough/rebuild_multiple_indexes_at_startup.js index 108860cfd6a..204dbe49596 100644 --- a/jstests/noPassthrough/rebuild_multiple_indexes_at_startup.js +++ b/jstests/noPassthrough/rebuild_multiple_indexes_at_startup.js @@ -23,8 +23,14 @@ return; } - let coll = rst.getPrimary().getDB("indexRebuild")["coll"]; - assert.commandWorked(coll.createIndexes([{a: 1}, {b: 1}], {}, {writeConcern: {w: "majority"}})); + let db = rst.getPrimary().getDB("indexRebuild"); + let coll = db["coll"]; + + assert.commandWorked(db.runCommand({ + createIndexes: "coll", + indexes: [{key: {a: 1}, name: "a"}, {key: {b: 1}, name: "b"}], + writeConcern: {w: "majority"} + })); assert.eq(3, coll.getIndexes().length); rst.awaitReplication(undefined, ReplSetTest.OpTimeType.LAST_DURABLE); diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js index 9aa901574fd..88331e14558 100644 --- a/src/mongo/shell/collection.js +++ b/src/mongo/shell/collection.js @@ -629,10 +629,18 @@ DBCollection.prototype._indexSpec = function(keys, options) { }; DBCollection.prototype.createIndex = function(keys, options) { + if (arguments.length > 2) { + throw new Error("createIndex accepts up to 2 arguments"); + } + return this.createIndexes([keys], options); }; DBCollection.prototype.createIndexes = function(keys, options) { + if (arguments.length > 2) { + throw new Error("createIndexes accepts up to 2 arguments"); + } + if (!Array.isArray(keys)) { throw new Error("createIndexes first argument should be an array"); } @@ -666,6 +674,10 @@ DBCollection.prototype.createIndexes = function(keys, options) { }; DBCollection.prototype.ensureIndex = function(keys, options) { + if (arguments.length > 2) { + throw new Error("ensureIndex accepts up to 2 arguments"); + } + var result = this.createIndex(keys, options); if (this.getMongo().writeMode() != "legacy") { |