Edited: New information added into the question
When using bulk operation with option journaling false it doesn't return code: 11000
duplicate key error.
When using following options, it doesn't return errors:
{ j:false }
{ w:0, j:false }
When using following options, it returns duplicate key
errors:
{ w:"majority", j:false }
{ w:0, j:true }
{ w:"majority", j:true }
I wonder that is this a correct behaviour?
Code for example:
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())
}
})
Additionally the schema of User model has an unique compound index as userSchema.index({ branch:1, department:1 }, { unique: true })
App Version
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