Mongorestore to update records if already exists without --drop

Shastry picture Shastry · Sep 22, 2014 · Viewed 13.6k times · Source

I have a huge dump of collection of data which i have to transfer to another machine every weekend. So i'm planning for an incremental backup and restore. As experimented, mongorestore never merges data if _id already exists. Based on the above problem i tried using mongoimport and export with but the same problem exists as the existing records are not merged. Any possible solution would be helpfull.

error in mongoimport command caused by :: 11000 E11000 duplicate key error index: news.news_data.$id dup key: { : ObjectId('5404410d9f5323ef734dac68') }

Answer

Kumar Kailash picture Kumar Kailash · Sep 22, 2014

The first case is true. Mongorestore does not update documents if already exist. It skipps those documnts when restoring. In your second case, please try using mongoimport with --upsert option. It will merge the records if _id already exists.

ex :

mongoimport --db dbname --collection collname --upsert --file file.json