Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongorestore to update records if already exists without --drop

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

like image 250
Shastry Avatar asked Sep 22 '14 06:09

Shastry


People also ask

Does Mongorestore overwrite data?

No. From mongorestore: If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.

Does Mongodump overwrite?

Overwrite Filesmongodump overwrites output files if they exist in the backup data folder. Before running the mongodump command multiple times, either ensure that you no longer need the files in the output folder (the default is the dump/ folder) or rename the folders or files.

How do I use Mongorestore?

Basic mongorestore syntax The basic way to restore a database is to use the mongorestore command to specify the backup directory (dump directory) without any options. This option is suitable for databases located in the localhost (127.0. 0.1) using the port 27017.

How do I restore a single collection in MongoDB?

To restore a single database or a collection (or specific documents) from a snapshot, you can use the Queryable Backup to export a single database or collection to restore to the target deployment.


1 Answers

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
like image 169
Kumar Kailash Avatar answered Oct 06 '22 23:10

Kumar Kailash