命令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
属性。
干杯!
版
答案 0 :(得分:7)
这里的独特性不是问题..它是_id,已经有索引(自动创建)而你不能创建具有完全相同字段的第二个索引(_id:1)第一个人有什么。
如果使用除了_id以外的其他字段进行测试,您会发现该字段的唯一性和背景是可能的,只要该字段不存在索引。
答案 1 :(得分:1)
在mongodb3.4中,_id字段不支持unique和background,其他字段也可以。
答案 2 :(得分:-1)
当我使用 sinon.stub 编写测试用例来调用函数时,我也发生了这个错误。 原因是,我在存根函数时传递了错误的数据。
代码 -(错误代码)
const accountstub = sinon.stub(accountservice, "getall");
accountstub.callFake(()=> accountmockdata)
已解决
const accountstub = sinon.stub(accountservice, "getall");
accountstub.callFake(()=> [accountmockdata])
解决方案 - 它需要一个对象数组,而我只传递了对象。 如果您在数据库中使用索引,请确保您通过的是正确的。
您可以通过在开发环境中运行来控制数据,然后查看函数返回的数据类型(对象、数组等)并将相同的数据粘贴到您的模拟文件中。