0

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
efkan
  • 12,991
  • 6
  • 73
  • 106
  • It does return the error. It's just that you are not serializing it correctly `console.log("result: \n%s", JSON.stringify(result,undefined,2))`. The response contains `writeErrors` which is an array of classed objects of type `writeError`. Your code should be showing you `[ WriteError { code: [Getter], ... toJSON: [Function],oString: [Function] } ]` Which means you just "deep serialize" as is shown. `console.log()` does not do that on it's own. Which is why you `JSON.stringify` **everything**, which is different to calling `.toJSON()` on a "top level" object. – Neil Lunn Oct 27 '17 at 21:23
  • Really a duplicate of [How can I display a JavaScript object?](https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object) but I initially marked as a duplicate of something else and too hastily removed the duplicate flag before realizing it's actually a duplicate of this one. – Neil Lunn Oct 27 '17 at 21:25
  • FYI. This initially "looked like" [MongoDB Node.js native driver silently swallows `bulkWrite` exception](https://stackoverflow.com/a/46700933/2313887), but as noted there at this level **BOTH** `(err, result)` actually contain the error detail, **BUT** this is changing in the next driver release. In the future the `result` will be empty and only `err` will contain a more informative `BulkWriteError`. – Neil Lunn Oct 27 '17 at 21:28
  • @NeilLunn **thank you very much** for information about `result` will be empty in the next driver. I didn't know. So I have to change right now, if I can.. – efkan Oct 28 '17 at 07:50
  • @NeilLunn , for you make sure I can say really it never returns `E11000` errors when setting options as `{ j:false }` and also I can get duplicate error if I change the setting as `{ j:true }`. However, I could not find any information about this issue on official documents. Maybe I have overlooked. Also `stringify` is not necessary because every `WriteError` object has a `toString()` method to serialize the error behalf of us ;) – efkan Oct 28 '17 at 07:56
  • Your code will be fine if you are specifically looking in "both" error and result for the relevant information. The natural thing is to look for the `BulkWriteResult` where present, and where `null` expect `BulkWriteError`, as opposed to the `MongoError` that returns presently. If you think you are still claiming nothing returns then it "actually does" just like I explain. I know because I have tested it. And it's also a unit test on the driver AFAIK. The only way you don't get a response is with "write concern" turned **off** completely, in "fire and forget" mode. And that's intended. – Neil Lunn Oct 28 '17 at 08:00
  • I realized that there is my fault. I use Mongoose, I was supposed to specify that. On the other hand Mongoose I have uses the latest version of the driver. I will edit the question in a short while after... – efkan Oct 28 '17 at 08:06
  • @NeilLunn , you remember correctly. I've found it. There is a [test](https://github.com/mongodb/node-mongodb-native/blob/2.2/test/functional/bulk_tests.js#L22) to check error code `11000`. But its db configuration has been set as `{w:1}`. Nevertheless I've [reported it](https://jira.mongodb.org/browse/NODE-1172). I guess we will know what is it.. – efkan Oct 28 '17 at 09:49

0 Answers0