mongodb 3.4.2 InvalidIndexSpecificationOption错误:字段'unique'对于_id索引规范无效

ice*_*awn 10 mongoose mongodb mongodb-indexes

该命令db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} )在mongo版本3.4.2上失败,但不是3.2.11.mongo文档表明版本3.4支持uniquebackground属性.

Mongo 3.4.2失败了......

> 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"
}
> 
Run Code Online (Sandbox Code Playgroud)

Mongo 3.2.11的作品......

> 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
}
> 
Run Code Online (Sandbox Code Playgroud)

有人知道一个解决方法吗?

我们使用Mongoose Node.js包装器来创建Mongo索引,因此不能添加uniquebackground属性.

干杯!

埃德

小智 7

这里唯一的问题不是..这就是_id,已经有索引(自动创建),你不能创建第二个索引具有完全相同的字段(_id:1)第一个具有的字段.

如何使用除_id之外的其他字段进行测试,您将发现该唯一且背景是可能的,只要该字段没有索引已经存在.