Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB inserting multiple documents where duplicate _id is possible

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.

like image 249
Joel Avatar asked Dec 14 '25 01:12

Joel


1 Answers

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) {
like image 179
Joel Avatar answered Dec 16 '25 18:12

Joel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!