0
0
MongodbHow-ToBeginner · 3 min read

How to Use mongodump for MongoDB Backups

Use mongodump to create a binary backup of your MongoDB database by running it from the command line. You can specify the database, collection, or output directory with options like --db, --collection, and --out.
📐

Syntax

The basic syntax of mongodump is:

  • mongodump [options]

Key options include:

  • --db <database>: Specify which database to dump.
  • --collection <collection>: Dump only a specific collection.
  • --out <directory>: Directory where dump files are saved.
  • --uri <connection-string>: MongoDB connection string to connect to a remote or local server.
bash
mongodump --db mydatabase --collection mycollection --out ./backup
💻

Example

This example shows how to dump the entire testdb database into a folder named dump in the current directory.

bash
mongodump --db testdb --out ./dump
Output
2024-06-01T12:00:00.000+0000 writing testdb.users to dump/testdb/users.bson 2024-06-01T12:00:00.100+0000 writing testdb.orders to dump/testdb/orders.bson Dump completed successfully.
⚠️

Common Pitfalls

Common mistakes when using mongodump include:

  • Not specifying the --db option and dumping all databases unintentionally.
  • Forgetting to set the correct --uri for remote servers, causing connection failures.
  • Using a directory for --out that does not have write permissions.
  • Expecting mongodump to export data in JSON format; it exports in BSON format.
bash
mongodump --db testdb --out /root/backup
# Error: permission denied

mongodump --db testdb --out ./backup
# Correct usage with writable directory
📊

Quick Reference

OptionDescription
--db Dump only the specified database
--collection Dump only the specified collection
--out Directory to save the dump files
--uri MongoDB connection string for remote or local server
--gzipCompress the dump files using gzip
--archive=Create a single archive file instead of a folder

Key Takeaways

Use mongodump to create binary backups of MongoDB databases or collections.
Specify the database with --db and output folder with --out for targeted backups.
mongodump exports data in BSON format, not JSON.
Ensure you have write permissions on the output directory to avoid errors.
Use --uri to connect to remote MongoDB instances securely.