How to fix mongorestore 'E11000 duplicate key error collection'
Problem:
You are trying to run mongorestore my-backup, but you see large numbers of warning messages like this one:
mongorestore-warning.txt
- E11000 duplicate key error collection: mydb.mycollection index: _id_ dup key: { : "MyKey" }Solution
By default, mongorestore does not overwrite or delete any existing documents. You need to tell it to drop each collection immediately before importing it from the backup:
mongorestore-drop.sh
mongorestore --drop my-backupAny documents not in the backup will be permanently lost after running this!
Note that this will not drop collections that are not present in the backup.
Also see mongodump/mongorestore minimal examples
Check out similar posts by category:
Databases
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow