Mongodb 3.4.2 InvalidIndexSpecificationOption error: "unique" field is not valid for _id index specification

The db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} ) fails in Mongo version 2.8.2, but not 3.2.11. The mongo documentation indicates that version 3.4 supports the unique and background attributes.

Mongo 3.4.2 fails ...

 > use testDB switched to db testDB > db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} ) { "ok" : 0, "errmsg" : "The field 'unique' is not valid for an _id index specification. Specification: { ns: \"testDB.testCollection\", v: 1, key: { _id: 1.0 }, name: \"_id_2\", unique: true, background: true }", "code" : 197, "codeName" : "InvalidIndexSpecificationOption" } > 

Mongo 3.2.11 works ...

 > use testDB switched to db testDB > db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} ) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 1, "note" : "all indexes already exist", "ok" : 1 } > 

Does anyone know about work?

We use the Mongoose Node.js shell to create Mongo indexes, so not adding unique and background attributes is not a parameter.

Hooray!

Ed

+5
source share
2 answers

This is a unique problem not here. It is that _id that already has an index (created automatically), and you cannot create a second index that has exactly the same fields (_id: 1) as the first.

How about testing with a field other than _id, and you will find that unique and background capabilities are possible if that field no longer has an index.

+2
source

in mongodb3.4, unique and background are not supported in the _id field, other fields are possible.

+1
source

Source: https://habr.com/ru/post/1264541/


All Articles