I'm using the following to insert an array of objects into a MongoDB(I'm assigning a unique _id), which is working:
collection.insert(records, {w:1}, function(err, result)
It's possible that some of the records could have a duplicate _id(meaning that record is already in the DB). Here is the error I get:
MongoError: E11000 duplicate key error index: heroku_app23495772.records.$_id_ dup key: { : "2b09aadb900f0e5112b6d03f665fb946" }
Will mongoDB still insert remaining objects in this array even after encountering the error on the first object(duplicate)? I really don't mind the error if it it won't prevent remaining documents that are not duplicates from being inserted or affect anything else negatively.
Or, should I literally query the DB for each object to see if it exists before sending the array of objects? I would think doing that would not be the best thing for performance.
I'm just trying to figure out the most efficient way of dealing with these duplicates.
Okay, I finally got this to work. Including continueOnError: true will continue inserting the rest of the batch even if some are not inserted because they are duplicates.
collection.insert(records, {continueOnError: true}, function(err, result) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With