`insertMany` ignore duplicate
See original GitHub issueI’ve a collection say Tweets
and each tweet has _id
and another unique id id_str
(added when creating schema).
When running insertMany
, is it possible to just ignore tweets with duplicate id_str
without throwing error? I tried following which does not work and still throw error:
try {
const tweets = fetchedFromApi()
await Tweet.insertMany(tweets, { ordered: false })
} catch (error) {
console.log(error) // -> MongoError: write operation failed
}
Node: v10.14.0
Mongoose: v5.3.13
MongoDB: v4.0.2
Issue Analytics
- State:
- Created 5 years ago
- Comments:13
Top Results From Across the Web
how to ignore duplicate documents when using insertMany in ...
Try to replace 'continueOnError' option by 'ordered' set to false, based on the documentation, when ordered option is set to false the ...
Read more >How to bulk insert documents and skip duplicates - MongoDB
Hey there,. How do I use insert_many() to insert a batch of documents, where some of the documents may contain a duplicate id....
Read more >db.collection.insertMany() — MongoDB Manual 3.4
The following attempts to insert multiple documents with _id field and ordered: false. The array of documents contains two documents with duplicate _id...
Read more >[mongodb-user] insert_many mongoDB and how to ignore ...
The code above was successfully ignoring the errors about duplicate keys, which is what I wanted. Now, I upgraded to MongoDB 4.0 and...
Read more >MongoDB insertMany and skip duplicates-mongodb
So you just wanted to skip duplicate docs as your intention is not update duplicate docs with latest data - So there is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@atulmy This issue came up in my head when doing bulkInsert myself now and i think what you were doing from the beginning on was technical correct. Setting
ordered: false
in theinsertMany
will insert ALL documents that are possible and “skip” those that throw an error. But when finished, the function itself will report/throw an error with all information on how many documents had been saved and how many not (see the docs: https://docs.mongodb.com/manual/reference/method/db.collection.insertMany ). So… i guess you were just thinking “it throws an error, so no documents were stored” maybe? But infact settingordered: false
will NOT abort the insert if errors happen, it will just report them at the end. So this should be the exact thing you wanted, am i right? Can you check if this works and you maybe misread the error-message? 😃 edit: Still, if you have the possibility to delete duplicated entries before the insert, that would be more clean. But sometimes, if you insert 10 million documents, checking duplicated ISd before the insert takes way longer than just inserting and ignoring the errros of duplucated entries.Please send a working code-example of what is working and what is not. I don’t fully understand your problem and the mistake might be somewhere else 😃