Guide to Exporting and Importing MongoDB Databases with `mongodump` and `mongorestore`

2 min read .

Database management is a crucial aspect of application development, especially when working with MongoDB, a popular NoSQL database. How to export and import MongoDB databases using two key commands: mongodump and mongorestore. These commands are essential for creating backups and restoring databases as needed.

1. Exporting MongoDB Databases with mongodump

Exporting a database is used to create a backup of the data from a MongoDB database. The mongodump command creates a backup by exporting all the data from the database into BSON (Binary JSON) format, which is then saved into the specified directory.

Basic syntax for exporting a database:

mongodump -d <database_name> -o <directory_backup>
  • -d <database_name>: Replace <database_name> with the name of the database you want to export.
  • -o <directory_backup>: Replace <directory_backup> with the destination directory where you want to store the backup.

Example:

Suppose you want to back up a database named mydatabase into a directory called backup:

mongodump -d mydatabase -o backup

After running this command, you will have a backup directory containing data from mydatabase.

2. Importing MongoDB Databases with mongorestore

Importing a database is used to restore data from a previously created backup. The mongorestore command imports data from BSON files into a MongoDB database.

Basic syntax for importing a database:

mongorestore -d <database_name> <directory_backup>
  • -d <database_name>: Replace <database_name> with the name of the target database where you want to restore the data.
  • <directory_backup>: Replace <directory_backup> with the directory where the backup is located.

Example:

Suppose you want to restore data into a database named mydatabase from the backup directory:

mongorestore -d mydatabase backup

This command will take all the data from the backup directory and import it into the mydatabase database.

3. Advanced Usage

  • Exporting and Importing Specific Collections: You can also export or import specific collections from a database using the -c option to specify the collection name.

    Example Export Collection:

    mongodump -d mydatabase -c mycollection -o backup

    Example Import Collection:

    mongorestore -d mydatabase -c mycollection backup/mydatabase/mycollection.bson
  • Additional Options: mongodump and mongorestore support various additional options such as authentication, dumping from remote hosts, or exporting only a subset of data based on specific queries.

4. Conclusion

Using mongodump and mongorestore is an efficient way to create backups and restore your MongoDB databases. This is crucial for maintaining data integrity and protecting data from loss. By following the steps above, you can easily export and import MongoDB databases according to your needs. Whether for backup purposes, migration, or data recovery, these commands are powerful tools in MongoDB database management.

Tags:
MongoDB

See Also

chevron-up