codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

36 / 65 topics
35Backup and Restore36mongodump and mongorestore37Replica Set Backup38Sharded Cluster Backup39Monitoring MongoDB40MongoDB Logs41Performance Tuning42Memory Management43Disk Space Optimization44Replica Set Maintenance45Sharded Cluster Maintenance
Tutorials/MongoDB/mongodump and mongorestore
🍃MongoDB

mongodump and mongorestore

Updated 2026-04-20
4 min read

Introduction

In this tutorial, we will explore mongodump and mongorestore, two essential tools for backing up and restoring your MongoDB databases. These utilities are crucial for maintaining data integrity, disaster recovery, and migrating data between environments.

What is mongodump?

mongodump is a utility that creates a binary export of the contents of a MongoDB database. It generates BSON files containing the data from each collection in the specified database. This backup can be used to restore your data using mongorestore.

What is mongorestore?

mongorestore is a utility that restores data from a backup created by mongodump. It reads BSON files and inserts them into the target MongoDB database, effectively restoring the state of the database at the time of the dump.

Prerequisites

  • Basic understanding of MongoDB concepts such as databases, collections, and documents.
  • MongoDB server installed and running.
  • Access to a terminal or command prompt.

Installing mongodump and mongorestore

mongodump and mongorestore are included with the MongoDB installation. Ensure that your MongoDB binaries are in your system's PATH so you can access them from any location.

To verify, run:

mongodump --version
mongorestore --version

If these commands return version information, the tools are correctly installed and accessible.

Basic Usage

mongodump

The basic syntax for mongodump is:

mongodump [options]

Common Options

  • --db <database>: Specifies the database to back up. If omitted, all databases are backed up.
  • --collection <collection>: Specifies a collection within the database to back up. If omitted, all collections in the specified database are backed up.
  • --out <directory>: Specifies the directory where the dump files will be stored. Defaults to the current directory.

Example

To back up the entire mydatabase database:

mongodump --db mydatabase

This command creates a directory named dump/mydatabase/, containing BSON files for each collection in mydatabase.

mongorestore

The basic syntax for mongorestore is:

mongorestore [options] <directory>

Common Options

  • --db <database>: Specifies the target database where the data will be restored. If omitted, the directory name is used as the database name.
  • --collection <collection>: Specifies a collection within the database to restore. If omitted, all collections in the specified directory are restored.
  • --drop: Drops the target database before restoring.

Example

To restore the data from the backup of mydatabase:

mongorestore --db mydatabase dump/mydatabase/

This command restores the contents of the BSON files into the mydatabase database.

Advanced Usage

Incremental Backups

MongoDB does not support incremental backups natively. However, you can achieve similar results by using logical backups with specific queries or by leveraging MongoDB Atlas' continuous backup feature.

Example: Backup Specific Documents

To back up only documents that match a query:

mongodump --db mydatabase --collection users --query '{ "status": "active" }'

Scheduled Backups

For regular backups, consider using cron jobs (on Unix-based systems) or Task Scheduler (on Windows).

Example: Cron Job for Daily Backup

Add the following line to your crontab:

0 2 * * * /usr/bin/mongodump --db mydatabase --out /backup/mydatabase/

This command runs mongodump daily at 2 AM, storing backups in /backup/mydatabase/.

Restoring Specific Collections

To restore only specific collections from a backup:

mongorestore --db mydatabase dump/mydatabase/users.bson

Best Practices

  1. Regular Backups: Schedule regular backups to prevent data loss.

  2. Test Restores: Regularly test your restore process to ensure it works as expected.

  3. Secure Backups: Store backups in a secure location, preferably offsite or in the cloud.

  4. Version Compatibility: Ensure that mongodump and mongorestore versions are compatible with your MongoDB server version.

  5. Compression: Use compression tools like gzip to reduce backup file size:

    mongodump --gzip --out /backup/
    
  6. Logging: Enable logging for both mongodump and mongorestore to track operations and diagnose issues.

Troubleshooting

Common Issues

  1. Permission Errors: Ensure that the user running mongodump or mongorestore has the necessary permissions.
  2. Network Issues: Verify network connectivity between your machine and the MongoDB server.
  3. File System Space: Ensure there is enough disk space to store the backup files.

Example: Handling Permission Errors

If you encounter permission errors, run the commands with elevated privileges:

sudo mongodump --db mydatabase

Conclusion

mongodump and mongorestore are powerful tools for managing MongoDB backups. By understanding their capabilities and best practices, you can ensure that your data is safe and recoverable in case of any issues.

Remember to regularly test your backup and restore processes to maintain data integrity and availability.


PreviousBackup and RestoreNext Replica Set Backup

Recommended Gear

Backup and RestoreReplica Set Backup