11
votes

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') }

2
Are the old records been replaced?Kumar Kailash
No. Because of error, old records remain unmerged.Shastry

2 Answers

16
votes

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

Example:

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

Please vote for this ticket to add an upsert option to mongorestore.

Until that is implemented, we have found a workaround:

  1. Dump Collection From Server A ( original )
  2. Dump Collection From Server B ( target )
  3. Restore With Drop Collection A on Server B 4
  4. Restore Without Drop Collection B on Server B

In this case updated document will not overwritten