Skip to content

Commit be07da4

Browse files
laplabEvergreen Agent
authored andcommitted
SERVER-26726 Check number of arguments for createIndex, createIndexes and ensureIndex shell commands
1 parent 5e03a3a commit be07da4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Tests that helper functions for creating indexes check the number of arguments passed.
2+
3+
(function() {
4+
"use strict";
5+
6+
const coll = db.create_index_helper_validation;
7+
coll.drop();
8+
9+
assert.throws(() => coll.createIndexes(
10+
/* keys */[{a: 1}],
11+
/* options */ {},
12+
/* commitQuorum */ "majority",
13+
{background: true},
14+
{unique: true}));
15+
16+
assert.throws(() => coll.createIndex(
17+
/* keys */ {a: 1},
18+
/* options */ {},
19+
/* commitQuorum */ "majority",
20+
{background: true},
21+
{unique: true}));
22+
23+
assert.throws(() => coll.ensureIndex(
24+
/* keys */ {a: 1},
25+
/* options */ {},
26+
/* commitQuorum */ "majority",
27+
{background: true},
28+
{unique: true}));
29+
}());

src/mongo/shell/collection.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,18 @@ DBCollection.prototype._indexSpec = function(keys, options) {
639639
};
640640

641641
DBCollection.prototype.createIndex = function(keys, options, commitQuorum) {
642+
if (arguments.length > 3) {
643+
throw new Error("createIndex accepts up to 3 arguments");
644+
}
645+
642646
return this.createIndexes([keys], options, commitQuorum);
643647
};
644648

645649
DBCollection.prototype.createIndexes = function(keys, options, commitQuorum) {
650+
if (arguments.length > 3) {
651+
throw new Error("createIndexes accepts up to 3 arguments");
652+
}
653+
646654
if (!Array.isArray(keys)) {
647655
throw new Error("createIndexes first argument should be an array");
648656
}
@@ -664,6 +672,10 @@ DBCollection.prototype.createIndexes = function(keys, options, commitQuorum) {
664672
};
665673

666674
DBCollection.prototype.ensureIndex = function(keys, options) {
675+
if (arguments.length > 2) {
676+
throw new Error("ensureIndex accepts up to 2 arguments");
677+
}
678+
667679
var result = this.createIndex(keys, options);
668680

669681
if (this.getMongo().writeMode() != "legacy") {

0 commit comments

Comments
 (0)