1. Backup MongoDB Database
✅ Option 1: Backup Entire Database
Use the mongodump command.
Syntax:
mongodump --db <database_name> --out <backup_directory>
Example:
C:\backup>set path=C:\Program Files\MongoDB\Server\8.2\bin
C:\backup>mongodump --db emp1 --out c:\backup
mongodump --db testdb --out C:\backup\
This will create:
C:\backup\testdb\
Containing .bson (data) and .metadata.json (indexes).
✅ Option 2: Backup All Databases
mongodump --out C:\backup\all_dbs\
C:\backup>mongodump --out c:\backup\alldbs
✅ Option 3: Backup with Authentication
If your MongoDB has authentication enabled:
mongodump --uri="mongodb://username:password@localhost:27017/testdb" --out C:\backup\
2. Restore MongoDB Database
Use the mongorestore command.
Syntax:
mongorestore --db <database_name> <backup_folder_path>
Example:
mongorestore --db testdb C:\backup\testdb\
This restores the testdb database from the backup folder.
✅ Option 2: Restore All Databases
mongorestore C:\backup\all_dbs\
✅ Option 3: Restore with Authentication
mongorestore --uri="mongodb://username:password@localhost:27017" C:\backup\
Useful Flags:
| Option | Description |
|---|---|
--drop | Drops existing collections before restoring. |
--gzip | Use if your backup was compressed. |
--verbose | Displays detailed output during restore. |
Example with --drop:
mongorestore --drop --db testdb C:\backup\testdb\
Example Backup & Restore Workflow
Step 1: Backup
mongodump --db studentDB --out C:\mongo_backup\
Step 2: Delete or modify data (simulate loss)
use studentDB
db.students.drop()
Step 3: Restore
mongorestore --db studentDB C:\mongo_backup\studentDB\
Backup & Restore Files Created
Each collection is saved as:
collection_name.bson— raw datacollection_name.metadata.json— index and metadata