Joel
Joel

Reputation: 665

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.

Upvotes: 2

Views: 4982

Answers (2)

Joel
Joel

Reputation: 665

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) {

Upvotes: 5

Tug Grall
Tug Grall

Reputation: 3520

The _id must be unique in the collection, so you cannot insert 2 documents with the same _id.

Not sure what is your exact use case but you have another approach, such as doing an upsert or save.

If you do a collection.save the document will be replaced: http://docs.mongodb.org/manual/reference/method/db.collection.save/

If you do an update, with the upsert:true you can control what needs to be updated or created: http://docs.mongodb.org/manual/reference/method/db.collection.update/#insert-a-new-document-if-no-match-exists-upsert

If this does not help, can you please give us the exact use case you are trying to achieve, and remember that if you do not provide any _id the system will generate one for you.

Upvotes: 0

Related Questions