13

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

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
Shastry
  • 436
  • 1
  • 5
  • 13

2 Answers2

20

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
Pang
  • 9,564
  • 146
  • 81
  • 122
Kumar Kailash
  • 1,385
  • 1
  • 10
  • 19
  • 1
    Be warned that mongoimport imports json, which doesn't have all the type information that mongorestore, which uses bson, will have. Also, I don't quite see what the problem is- if you're transferring a dump, why not drop the old data so there's no duplicates? – wdberkeley Sep 22 '14 at 18:31
  • 3
    @wdberkeley: some potential reasons for preferring an [upsert](https://jira.mongodb.org/browse/TOOLS-121) to a drop + restore: only a few documents are updated in a large collection; not wanting to waste time recreating indexes; emitting only the necessary changes for changeStreams. – Dan Dascalescu Jun 10 '19 at 17:38
6

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

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404