0

I want to bulk insert documents and I'm creating the _id. Is there a way to bulk over write the existing documents with new documents if _id matches?

KaliCharan
  • 1,215
  • 2
  • 8
  • 12
  • Mongo index has a feature to drop duplicates: https://stackoverflow.com/questions/13190370/how-to-remove-duplicates-based-on-a-key-in-mongodb – Agoston Horvath May 08 '19 at 13:26
  • dropDups is dropped long time ago and is not available in any maintainable versions of the db. Use bulk upserts instead https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/#updateone-and-updatemany – Alex Blex May 08 '19 at 13:38
  • Say this is my first insert. { "_id" : 1, "char" : "Brisbane", "class" : "monk", "lvl" : 4 }, { "_id" : 2, "char" : "Eldon", "class" : "alchemist", "lvl" : 3 }, My use case is to override the existing values . The following insert shouldn't throw error but overwrite the char fields { "_id" : 1, "char" : "England", "class" : "monk", "lvl" : 4 }, { "_id" : 2, "char" : "Scotland", "class" : "alchemist", "lvl" : 3 }, – KaliCharan May 08 '19 at 13:58
  • Did you tried MongoDB db.collection.update(query,dataToUpdate, option) function with option {upsert: true}. It will first find the document and update if exist or insert a new document if the query does not match. – Jitendra May 09 '19 at 08:22
  • https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-option – Jitendra May 09 '19 at 08:24
  • Update: Nothing of this sort exists as per the current mongo functionality. – KaliCharan May 28 '19 at 10:21

0 Answers0