CHAPTER 11
In this chapter, you will learn how to back up and restore a database in MongoDB.
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:

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.
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 |

Figure 49: Restoring the database.