mongodump/mongorestore minimal examples
Create & restore a database
mongodump.sh
mongodump --db mydb --out mydb.mongobackupThis will backup the database mydb from the MongoDB running at localhost and store the backup in the newly created DigiKey.mongobackup directory as BSON.
mongorestore.sh
mongorestore mydb.mongobackupThis will restore the backup to localhost (the database name, mydb, is stored in the backup directory).
It will not overwrite or update existing documents, nor delete documents that are currently present but not present in the backup.
Restore backup with drop
mongorestore-drop.sh
mongorestore --drop mydb.mongobackupThis will drop (i.e. delete) each collection before importing from the backup. This means that
- Existing documents will effectively be overwritten
- Documents that are currently present but not present in the backup will be deleted
However note that while importing the backup, some documents might be missing from the database until the backup has been fully restored.
Note that this will not drop collections that are not present in the backup.
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