Is there a way for mongorestore to replace the records existing in the database instead of skipping it which is the default behavior ?
I\'m currently using the lates
You could use the --drop parameter of mongorestore
:
Before restoring the collections from the dumped backup, drops the collections from the target database. --drop does not drop collections that are not in the backup.
I have solved this using
mongorestore --db database -c collection ~/path_to_bson > output.txt 2>&1
- E11000 duplicate key error collection ...ObjectId(...)
db.collection.remove({_id: ObjectId(...)})
If you have a small collection you could always:
This switches the precedence to your dump file instead of to the _ids in the DB.
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.
If you want to completely overwrite the database or a collection within the database with mongorestore, drop the database or collection (check --drop parameter as pointed out by vikas's answer). If you want to replace certain documents or merge duplicate documents, then you'll need to write your own script to do the restore and implement the more complex logic yourself.