summaryrefslogtreecommitdiff
path: root/jstests/core
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/core')
-rw-r--r--jstests/core/create_collection.js12
-rw-r--r--jstests/core/index_id_desc.js34
-rw-r--r--jstests/core/index_id_options.js131
3 files changed, 69 insertions, 108 deletions
diff --git a/jstests/core/create_collection.js b/jstests/core/create_collection.js
index c7858f5f902..2af8bc721a2 100644
--- a/jstests/core/create_collection.js
+++ b/jstests/core/create_collection.js
@@ -52,11 +52,13 @@
db.createCollection("create_collection", {idIndex: {key: {a: 1}, name: "_id_"}}),
ErrorCodes.BadValue);
- // "idIndex" field must have name equal to "_id_".
+ // The name of an _id index gets corrected to "_id_".
db.create_collection.drop();
- assert.commandFailedWithCode(
- db.createCollection("create_collection", {idIndex: {key: {_id: 1}, name: "a_1"}}),
- ErrorCodes.BadValue);
+ assert.commandWorked(
+ db.createCollection("create_collection", {idIndex: {key: {_id: 1}, name: "a_1"}}));
+ var indexSpec = GetIndexHelpers.findByKeyPattern(db.create_collection.getIndexes(), {_id: 1});
+ assert.neq(indexSpec, null);
+ assert.eq(indexSpec.name, "_id_", tojson(indexSpec));
// "idIndex" field must only contain fields that are allowed for an _id index.
db.create_collection.drop();
@@ -69,7 +71,7 @@
db.create_collection.drop();
assert.commandWorked(
db.createCollection("create_collection", {idIndex: {key: {_id: 1}, name: "_id_"}}));
- var indexSpec = GetIndexHelpers.findByName(db.create_collection.getIndexes(), "_id_");
+ indexSpec = GetIndexHelpers.findByName(db.create_collection.getIndexes(), "_id_");
assert.neq(indexSpec, null);
assert.eq(indexSpec.v, 2, tojson(indexSpec));
diff --git a/jstests/core/index_id_desc.js b/jstests/core/index_id_desc.js
deleted file mode 100644
index 515a422163e..00000000000
--- a/jstests/core/index_id_desc.js
+++ /dev/null
@@ -1,34 +0,0 @@
-// Test creation of an index with key pattern {_id: -1}. It is expected that a request for creation
-// of a {_id: -1} index is treated as if it were a request for creation of a {_id: 1} index.
-// SERVER-14833.
-
-var coll = db.index_id_desc;
-var indexes;
-
-// Test ensureIndex({_id: -1}) on a nonexistent collection.
-coll.drop();
-assert.commandWorked(coll.ensureIndex({_id: -1}));
-indexes = coll.getIndexes();
-assert.eq(1, indexes.length);
-assert.eq("_id_", indexes[0].name);
-assert.eq({_id: 1}, indexes[0].key);
-
-// Test ensureIndex({_id: -1}) on a normal empty collection.
-coll.drop();
-assert.commandWorked(coll.runCommand("create"));
-assert.eq(1, coll.getIndexes().length);
-assert.commandWorked(coll.ensureIndex({_id: -1}));
-indexes = coll.getIndexes();
-assert.eq(1, indexes.length);
-assert.eq("_id_", indexes[0].name);
-assert.eq({_id: 1}, indexes[0].key);
-
-// Test ensureIndex({_id: -1}) on an empty collection with no _id index.
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.eq(0, coll.getIndexes().length);
-assert.commandWorked(coll.ensureIndex({_id: -1}));
-indexes = coll.getIndexes();
-assert.eq(1, indexes.length);
-assert.eq("_id_", indexes[0].name);
-assert.eq({_id: 1}, indexes[0].key);
diff --git a/jstests/core/index_id_options.js b/jstests/core/index_id_options.js
index 5333c12daa8..a387c409b6d 100644
--- a/jstests/core/index_id_options.js
+++ b/jstests/core/index_id_options.js
@@ -1,79 +1,72 @@
// Test creation of the _id index with various options:
-// - _id indexes must be unique.
-// - _id indexes can't be sparse.
-// - _id indexes can't be partial indexes.
+// - _id indexes must have key pattern {_id: 1}.
+// - The name of an _id index gets corrected to "_id_".
+// - _id indexes cannot have any options other than "key", "name", "ns", "v", and "collation".
+// - _id indexes must have the collection default collation.
+// - Non-_id indexes cannot have the name "_id_".
-var coll = db.index_id_options;
+(function() {
+ "use strict";
-//
-// Unique index.
-//
+ load("jstests/libs/get_index_helpers.js");
-// Creation of _id index with "non-zero" value for "unique" should succeed.
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandWorked(coll.ensureIndex({_id: 1}, {unique: true}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandWorked(coll.ensureIndex({_id: 1}, {unique: 1}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandWorked(coll.ensureIndex({_id: 1}, {unique: NumberLong(1)}));
+ var coll = db.index_id_options;
-// Creation of _id index with "zero" value for "unique" should fail.
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {unique: false}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {unique: 0}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {unique: NumberLong(0)}));
+ // _id indexes must have key pattern {_id: 1}.
+ coll.drop();
+ assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
+ assert.commandFailed(coll.ensureIndex({_id: -1}, {name: "_id_"}));
-//
-// Sparse index.
-//
+ // The name of an _id index gets corrected to "_id_".
+ coll.drop();
+ assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
+ assert.commandWorked(coll.ensureIndex({_id: 1}, {name: "bad"}));
+ var spec = GetIndexHelpers.findByKeyPattern(coll.getIndexes(), {_id: 1});
+ assert.neq(null, spec, "_id index spec not found");
+ assert.eq("_id_", spec.name, tojson(spec));
-// Creation of _id index with "non-zero" value for "sparse" should fail.
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {sparse: true}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {sparse: 1}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {sparse: NumberLong(1)}));
+ // _id indexes cannot have any options other than "key", "name", "ns", "v", and "collation."
+ coll.drop();
+ assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", unique: true}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", sparse: false}));
+ assert.commandFailed(
+ coll.ensureIndex({_id: 1}, {name: "_id_", partialFilterExpression: {a: 1}}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", expireAfterSeconds: 3600}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", background: false}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", unknown: true}));
+ assert.commandWorked(coll.ensureIndex(
+ {_id: 1}, {name: "_id_", ns: coll.getFullName(), v: 2, collation: {locale: "simple"}}));
-// Creation of _id index with "zero" value for "unique" should succeed.
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandWorked(coll.ensureIndex({_id: 1}, {sparse: false}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandWorked(coll.ensureIndex({_id: 1}, {sparse: 0}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandWorked(coll.ensureIndex({_id: 1}, {sparse: NumberLong(0)}));
+ // _id indexes must have the collection default collation.
+ coll.drop();
+ assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", collation: {locale: "en_US"}}));
+ assert.commandWorked(coll.ensureIndex({_id: 1}, {name: "_id_", collation: {locale: "simple"}}));
-//
-// Partial index.
-//
+ coll.drop();
+ assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
+ assert.commandWorked(coll.ensureIndex({_id: 1}, {name: "_id_"}));
-// Creation of _id index with any value for "partialFilterExpression" should fail.
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {partialFilterExpression: false}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {partialFilterExpression: null}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {partialFilterExpression: {}}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {partialFilterExpression: {a: 1}}));
-coll.drop();
-assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
-assert.commandFailed(coll.ensureIndex({_id: 1}, {partialFilterExpression: []}));
+ coll.drop();
+ assert.commandWorked(
+ coll.runCommand("create", {autoIndexId: false, collation: {locale: "en_US"}}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", collation: {locale: "simple"}}));
+ assert.commandFailed(coll.ensureIndex({_id: 1}, {name: "_id_", collation: {locale: "fr_CA"}}));
+ assert.commandWorked(
+ coll.ensureIndex({_id: 1}, {name: "_id_", collation: {locale: "en_US", strength: 3}}));
+
+ coll.drop();
+ assert.commandWorked(
+ coll.runCommand("create", {autoIndexId: false, collation: {locale: "en_US"}}));
+ assert.commandWorked(coll.ensureIndex({_id: 1}, {name: "_id_"}));
+ spec = GetIndexHelpers.findByName(coll.getIndexes(), "_id_");
+ assert.neq(null, spec, "_id index spec not found");
+ assert.eq("en_US", spec.collation.locale, tojson(spec));
+
+ // Non-_id indexes cannot have the name "_id_".
+ coll.drop();
+ assert.commandWorked(coll.runCommand("create", {autoIndexId: false}));
+ assert.commandFailed(coll.ensureIndex({_id: "hashed"}, {name: "_id_"}));
+ assert.commandFailed(coll.ensureIndex({a: 1}, {name: "_id_"}));
+})();