...
For the most part, ensureIndex on an existing index but with different options error, but if the only difference is the name, it doesn't error. > db.test.ensureIndex( { foo: 1 } ) WriteResult({ "nInserted" : 1 }) > db.test.ensureIndex( { foo: 1 }, { unique: true } ) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 67, "errmsg" : "Index with name: foo_1 already exists with different options" } }) > db.test.ensureIndex( { foo: 1 }, { name: "kay"} ) WriteResult({ "nInserted" : 0 }) > db.test.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.test" }, { "v" : 1, "key" : { "foo" : 1 }, "name" : "foo_1", "ns" : "test.test" } ] Given that the check seems to be by name, I can see that if the names differ, then of course, it won't do the check but wasn't sure. If this is the expected behavior, I can just simply state that if an index already exists with the same name, ensureIndex will error if the key specs or the options differ. instead of having two separate error cases of (same key spec, different options) = error and (different key spec, same name) = error
david.storch commented on Fri, 5 Jul 2019 18:44:21 +0000: It looks like this has been fixed under SERVER-33149. Resolving as a duplicate. vak commented on Thu, 18 Feb 2016 11:24:54 +0000: this restriction prohibits creation of ver1 indices without dropping ver0 rassi@10gen.com commented on Wed, 26 Mar 2014 21:41:49 +0000: Re-opening, this didn't go away in rc2; I think you two are confused. rassi@laptop:~/work/mongo $ mongo260rc2 MongoDB shell version: 2.6.0-rc2 connecting to: test > db.version() 2.6.0-rc2 > db.foo.drop() false > db.foo.insert({foo:1}) WriteResult({ "nInserted" : 1 }) > db.foo.ensureIndex({foo:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.foo.ensureIndex({foo:1},{name:"kay"}) { "numIndexesBefore" : 2, "note" : "all indexes already exist", "ok" : 1 } > db.foo.ensureIndex({foo:1},{unique:true}) { "ok" : 0, "errmsg" : "Index with name: foo_1 already exists with different options", "code" : 67 } > The issue Kay is reporting here is that db.foo.ensureIndex({foo:1},{name:"kay"}) should return an error, since an index with name "kay" can't be created due to a key pattern conflict with an existing index. kay.kim@10gen.com commented on Tue, 25 Mar 2014 23:49:35 +0000: dan@10gen.com seems to be fixed in rc2. dan@10gen.com commented on Tue, 25 Mar 2014 04:26:34 +0000: I can't repro with 2.6.0-rc2. kay.kim@10gen.com, can you check again?