已编辑:已在问题中添加新信息
使用带日期选项日记功能的批量操作时,它不会返回code: 11000
重复键错误。
使用以下选项时,不会返回错误:
{ j:false }
{ w:0, j:false }
使用以下选项时,会返回 duplicate key
错误:
{ w:"majority", j:false }
{ w:0, j:true }
{ w:"majority", j:true }
我想知道这是正确的行为吗?
代码例如:
var ObjectId = mongoose.Types.ObjectId,
User = require('./models/user')
let bulk = User.collection.initializeUnorderedBulkOp()
let doc1 = {
"branch" : "DUPLICATE",
"department" : ObjectId("582bf1d8322809041e667777"),
}
let doc2 = {
"branch" : "TEST_SUCCESS",
"department" : ObjectId("582bf1d8322809041e668888"),
}
let doc3 = {
"branch" : "DUPLICATE",
"department" : ObjectId("582bf1d8322809041e669999"),
}
bulk.insert(doc1)
bulk.insert(doc2)
bulk.insert(doc3)
let bulkOptions = {
w:0,
j:false // next run change argument j as true
}
bulk.execute(bulkOptions, (err, result) => {
let writeErrors = result.toJSON().writeErrors
for (let i = 0; i > writeErrors.length - 1; i++) {
let error = writeErrors[i]
debug(error.toString())
}
})
此外,User模型的模式具有唯一的复合索引userSchema.index({ branch:1, department:1 }, { unique: true })
应用版本
mongoDB v3.4.3 (storageEngine is wiredTiger)
mongoose v4.12.4 (the documentation refers to node-mongodb-native API 2.2)
node.js v8.8.1