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') }
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With