CHAPTER 11
Back Up and Restore
In this chapter, you will learn how to back up and restore a database in MongoDB.
Back up
MongoDB provides a tool called mongodump.exe to back up your MongoDB database. By calling the mongodump --help, you will get quite a big list of options that can be used.
The most basic example would be backing up a database into a specific folder, which can be achieved as follows:
Code Listing 114: mongodump usage
mongodump -h localhost --db mydb -o c:\backup |
Where:
- -h represents the host where the MongoDB runs.
- --db represents the database to be backed up.
- -o contains the output folder.

Figure 47: Backup command.
After running the command on the filesystem, we can see that the data has been exported correctly, and that MongoDB has created a folder called mydb.

Figure 48: Backup folder.
The backup folder contains two types of files: .bson, which contains a bson copy of the data (bson being in binary format is not human-readable), and *metadata.json, which contains the configuration information of the collection itself, such as indexes.
Restore
After backing up the data, you can also restore it to MongoDB. To restore the data, you use mongorestore.exe. If you type it in the Command Prompt window and press Enter, you will get a response as shown in Figure 49.
The basic operation to perform is to restore the database from the previously taken backup. This is obtained by running the following command:
Code Listing 115: mongorestore usage
mongorestore -h localhost --db mydb --drop c:\backup |
- -h represents the host where the MongoDB runs.
- --db represents the database to be restored.
- --drop contains the information of dropping the collection before recreating it.

Figure 49: Restoring the database.
- 1800+ high-performance UI components.
- Includes popular controls such as Grid, Chart, Scheduler, and more.
- 24x5 unlimited support by developers.