question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`insertMany` ignore duplicate

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:13

github_iconTop GitHub Comments

2reactions
mhombachcommented, Dec 13, 2018

@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 the insertMany 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 setting ordered: 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.

0reactions
mhombachcommented, Mar 6, 2020

!!Need a help.I have added 1000000 numbers and make ordered:false if it have duplicate value in every 1000 items less than 200 it work fine.But if more than 500 duplicates in 1000 it gave a error and stop the insert other values.

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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found