⬅ Previous Topic
Backing Up MongoDB using mongodumpNext Topic ⮕
Creating Users and Assigning Roles in MongoDB⬅ Previous Topic
Backing Up MongoDB using mongodumpNext Topic ⮕
Creating Users and Assigning Roles in MongoDBmongorestore
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.
mongorestore [options] <backupFolderPath>
By default, mongorestore
restores to a running instance on localhost:27017
.
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
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
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.
If you want to fully replace the existing database:
mongorestore --drop --db=inventory backup/inventory
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.
You can also restore just one collection from the backup:
mongorestore --db=inventory --collection=products backup/inventory/products.bson
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.
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.
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.
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.
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.
⬅ Previous Topic
Backing Up MongoDB using mongodumpNext Topic ⮕
Creating Users and Assigning Roles in MongoDBYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.