Mongorestore to update entries if they already exist without --drop

I have a huge data collection dump that I have to transfer to another machine every weekend. Therefore, I am planning an incremental backup and restore. As experimentally, mongorestore never combines data if _id already exists. Based on the above problem, I tried to use mongoimport and export, but the same problem exists since existing records are not merged. Any possible solution would be helpful.

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

+7
source share
2 answers

The first case is true. Mongorestore does not update documents if they already exist. He restores these dokmas upon restoration. In the second case, try using mongoimport with the --upsert option. It will merge records if _id already exists.

ex:

mongoimport --db dbname --collection collname --upsert --file file.json
+13
source

Please vote for this ticket to add the option upsertto mongorestore .

Until this is implemented, we have found a workaround:

  1. Dumping from server A (original)
  2. Dumping from server B (target)
  3. Restore using Drop Collection A on server B 4
  4. Recover without resetting collection B on server B

In this case, the updated document will not be overwritten.

+1
source

All Articles