diff options
author | Jason Rassi <rassi@10gen.com> | 2014-08-11 12:43:57 -0400 |
---|---|---|
committer | Jason Rassi <rassi@10gen.com> | 2014-08-11 12:47:18 -0400 |
commit | 98eb2f1dd5e9577db16f2f109b0d8b5b9d56552c (patch) | |
tree | 3230e4d390ace00b063343a9d06f64b98ce7c290 | |
parent | aaa8a1d71e00bde3f9783dfaf0070c214e8ad1a1 (diff) | |
download | mongo-98eb2f1dd5e9577db16f2f109b0d8b5b9d56552c.tar.gz |
SERVER-14833 {_id:-1} index builds should no-op if _id index exists
-rw-r--r-- | jstests/index_id_desc.js | 38 | ||||
-rw-r--r-- | src/mongo/db/index.cpp | 21 |
2 files changed, 48 insertions, 11 deletions
diff --git a/jstests/index_id_desc.js b/jstests/index_id_desc.js new file mode 100644 index 00000000000..6b81105e8c6 --- /dev/null +++ b/jstests/index_id_desc.js @@ -0,0 +1,38 @@ +// 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; +var res; + +// Test ensureIndex({_id: -1}) on a nonexistent collection. +coll.drop(); +res = coll.ensureIndex({_id: -1}); +assert.isnull(res); +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); +res = coll.ensureIndex({_id: -1}); +assert.isnull(res); +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); +res = coll.ensureIndex({_id: -1}); +assert.isnull(res); +indexes = coll.getIndexes(); +assert.eq(1, indexes.length); +assert.eq("_id_", indexes[0].name); +assert.eq({_id: 1}, indexes[0].key); diff --git a/src/mongo/db/index.cpp b/src/mongo/db/index.cpp index e4366977081..294629dd945 100644 --- a/src/mongo/db/index.cpp +++ b/src/mongo/db/index.cpp @@ -360,6 +360,14 @@ namespace mongo { uasserted(12504, s); } + /* this is because we want key patterns like { _id : 1 } and { _id : -1 } to + all be treated as the same pattern. + */ + if ( IndexDetails::isIdIndexPattern(key) ) { + key = id_obj; + name = "_id_"; + } + sourceCollection = nsdetails(sourceNS); if( sourceCollection == 0 ) { // try to create it @@ -393,9 +401,6 @@ namespace mongo { uasserted(12505,s); } - /* this is because we want key patterns like { _id : 1 } and { _id : <someobjid> } to - all be treated as the same pattern. - */ if ( IndexDetails::isIdIndexPattern(key) ) { //if( !god ) { //ensureHaveIdIndex( sourceNS.c_str(), mayInterrupt ); @@ -439,14 +444,8 @@ namespace mongo { } // idea is to put things we use a lot earlier b.append("v", v); - if ( IndexDetails::isIdIndexPattern(o["key"].Obj()) ) { - b.append("name", "_id_"); - b.append("key", id_obj); - } - else { - b.append( o["name"] ); - b.append(o["key"]); - } + b.append("name", name); + b.append("key", key); if( o["unique"].trueValue() ) b.appendBool("unique", true); // normalize to bool true in case was int 1 or something... b.append(o["ns"]); |