🍃 MongoDB Tutorial

MongoDB Backup & Restore: mongodump and mongorestore Explained

Learn how to back up and restore MongoDB data at every level — the whole server, a single database, or just one collection — using mongodump and mongorestore.

Backing Up and Restoring MongoDB

MongoDB ships with two command-line tools for backups: mongodump (exports data to .bson files) and mongorestore (loads it back in). Both run from your regular terminal — not inside the mongosh shell.

Dump / Restore Everything

mongodump
mongorestore

Running mongodump with no arguments backs up every database on the server into a dump/ folder in your current directory.

Dump / Restore a Single Database

mongodump -d rahultechnology -o ./
mongodump -d rahultechnology -o /home/rahul/Music/
mongorestore -d rahultechnology1 --dir rahultechnology/
mongorestore -d rahultechnology2 ./
mongorestore -d rahultechnology4 /home/rahul/Pictures/rahultechnology/

-d sets the target database name for the restore — it doesn't have to match the original name, which is exactly how you'd rename a database (dump, then restore under a new name).

Dump / Restore a Single Collection

mongodump --db rahultechnology --collection employees
mongorestore --db rahultechnology --collection emp employees.bson
mongorestore --db rahultechnology --collection emp ./dump/rahultechnology/employees.bson

Notice the collection name can change between dump and restore (employees → emp) — MongoDB just imports whatever .bson file you point it at, under whatever name you choose.

Common Errors & Troubleshooting

  • Running mongodump/mongorestore inside mongosh — these are separate command-line tools, run from your regular terminal/shell, not typed into the MongoDB shell prompt.
  • mongorestore failing with "collection already exists" — by default, mongorestore won't overwrite an existing collection's data. Pass --drop if you want it to replace the target collection entirely first.
  • Authentication errors on a remote/Atlas cluster — remember to include connection credentials, e.g. mongodump --uri="mongodb+srv://user:pass@cluster.mongodb.net/dbname", rather than assuming a local, unauthenticated connection.

Key Takeaways

  • mongodump/mongorestore run from your OS shell, not from inside mongosh.
  • You can back up an entire server, a single database, or a single collection.
  • The database or collection name doesn't have to match on restore — this is the standard trick for renaming a MongoDB database.