SERVER-14833 {_id:-1} index builds should no-op if _id index exists

This commit is contained in:
Jason Rassi 2014-08-11 12:43:57 -04:00
parent aaa8a1d71e
commit 98eb2f1dd5
2 changed files with 48 additions and 11 deletions

38
jstests/index_id_desc.js Normal file
View File

@ -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);

View File

@ -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"]);