Mongorestore to update records if already exists without --drop Mongorestore to update records if already exists without --drop mongodb mongodb

Mongorestore to update records if already exists without --drop


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


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