admin管理员组

文章数量:1384140

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.

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.

Share Improve this question edited Mar 30, 2014 at 13:35 Joel asked Mar 30, 2014 at 13:29 JoelJoel 6852 gold badges9 silver badges23 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

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

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/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/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.

本文标签: javascriptMongoDB inserting multiple documents where duplicate id is possibleStack Overflow