【发布时间】:2017-07-11 16:59:31
【问题描述】:
命令 db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} ) 在 mongo 版本 3.4.2 上失败,但不是 3.2.11。 mongo 文档表明 3.4 版同时支持 unique 和 background 属性。
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"
}
>
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
}
>
有人知道解决方法吗?
我们正在使用 Mongoose Node.js 包装器来创建 Mongo 索引,因此不能添加 unique 和 background 属性。
干杯!
埃德
【问题讨论】:
-
如何解决这个问题?
标签: mongodb mongoose mongodb-indexes