Restoring Data using mongorestore
mongorestore
is a powerful utility provided by MongoDB to restore databases from binary backup files created using mongodump
. It is essential for database recovery, migrations, or setting up staging environments from production backups.
When Do You Use mongorestore?
- Recovering from accidental data loss
- Moving data to a different environment (e.g., from development to production)
- Cloning data between MongoDB servers
Basic Syntax
mongorestore [options] <backupFolderPath>
By default, mongorestore
restores to a running instance on localhost:27017
.
Example 1: Restore a Full Database
Assume you've previously created a backup using:
mongodump --db=inventory --out=backup/
This creates a folder backup/inventory
containing BSON and metadata files.
To restore it:
mongorestore --db=inventory backup/inventory
Output:
2025-05-01T08:15:00.123+0530 preparing collections to restore from 2025-05-01T08:15:00.145+0530 reading metadata for inventory.products from backup/inventory/products.metadata.json 2025-05-01T08:15:00.234+0530 restoring inventory.products from backup/inventory/products.bson 2025-05-01T08:15:00.456+0530 finished restoring inventory.products (1000 documents) restore completed successfully
What Happens If the Database Already Exists?
Q: Will mongorestore overwrite an existing database?
A: Yes, by default it adds to existing collections. If you want to overwrite everything, use the --drop
option.
Example 2: Restore with Drop Option
If you want to fully replace the existing database:
mongorestore --drop --db=inventory backup/inventory
Output:
dropped collection inventory.products restoring inventory.products from backup/inventory/products.bson finished restoring inventory.products (1000 documents)
This ensures all data is reset to what existed during the backup.
Example 3: Restoring a Specific Collection
You can also restore just one collection from the backup:
mongorestore --db=inventory --collection=products backup/inventory/products.bson
Output:
restoring inventory.products from backup/inventory/products.bson finished restoring inventory.products (1000 documents)
This is useful if you only need to recover a corrupted or deleted collection without affecting others.
Example 4: Restoring to a Remote MongoDB Server
If your MongoDB server is running remotely, specify the host and port:
mongorestore --host=192.168.1.100 --port=27018 --db=inventory backup/inventory
Use --username
and --password
for authenticated servers.
Example 5: Restore All Databases from Dump
If your dump folder contains multiple databases, you can restore all at once:
mongorestore backup/
This restores all databases that were dumped into the backup/
folder.
Frequently Asked Questions
Q: Can I restore into a different database name?
A: Yes. Use --nsFrom
and --nsTo
to rename namespaces:
mongorestore --nsFrom=inventory.* --nsTo=new_inventory.* backup/inventory
Q: Is it possible to restore only documents that match a condition?
A: No, mongorestore
doesn’t support filtering. You can restore to a temp DB and use aggregation or queries to copy what you need.
Summary
The mongorestore
command is essential for restoring MongoDB data from binary backups. You can restore full databases, specific collections, or entire environments using various options like --db
, --drop
, --collection
, and --host
.
In the next topic, we’ll look at how to secure your MongoDB server using user authentication and roles.