Published on December 31st, 2020
There have been a whole host of tools that have been built to make the process of maintaining databases easier.
Using these powerful tools and commands, complex operations that have to be done repeatedly are accomplished quickly and cleanly.
A single command can back up or restore the entire database, or specified portions of it.
When working with MongoDB databases (collections), you can use mongodump to accomplish this.
In this guide, we are going to walk you through what mongodump and what mongorestore are, how to use them, and provide some clear examples along the way so that you can use both tools to back up and restore your collections easily.
Mongodump works as a utility to take the contents of a database and create a binary export.
The tool is used with mongod and mongos instances. Running mongodump allows the user to export data from a standalone, replica, set, and sharded cluster deployments.
Mongodump used to be updated, and new versions were released whenever the MongoDB Server itself was updated. However, since, MongoDB 4.4, the utility has separate versioning.
The current version is 100.2.1, which supports MongoDB 3.6, 4.0, 4.2, and 4.4. Mongodump may not have full compatibility with all the earlier versions of MongoDB even though they are supported.
The tool serves as a backup strategy. For IT professionals looking to schedule backups of databases on a daily basis, this one of the methods for them to back up and restore databases (collections).
Mongodump can save everything in a single file, while mongorestore can later be used to completely restore the database.
You can run the mongodump command from the system command line, not the mongo shell.
This is the general mongodump command structure:
mongodump <options> <connection-string>
The user can connect to a mongo database using the
--uri and a correctly formatted string or flag options like
--password. The user isn't allowed to combine the two into a single command.
While using the localhost, mongodump is able to dump a collection called
redbase with the following command while using a URI format and the following user information:
Another example mongodump command using the standard flags would look like this:
mongodump --user=uberuser --db=redbase --password=passherd --authenticationDatabase=admin
It is also possible to run the database backup to an archive file. This is in contrast to dumping the files into a directory. These options are meant for transfering data between hosts or switching servers.
The --archive flag makes it possible to specify the name of the archive. The option creates one file that can be used to reimport the database with mongorestore.
If the name of the database is the same as the database that needs to be dumped, use the
--authenticationDatabase flag with the right name or if you are using URI, make sure the
authSource part of the string points to the right database.
The standard mongodump process involves dumping the entire database into a single
dump director which is named
dump by default.
This working directory will be placed in the working directory that your ran the command in. The directory has a sub-folder that is named after the database.
In the previous example, this would be
redbase so the new structure looks like
Two different files for the collection in the database will be in the specific folder. This includes a BSON file, and a JSON file.
Following the same pattern, the
<collection>.metadate.json file will contain the metadata like
ns to correspond with the namespace for the collection.
The BSON file contains the
<collection>.bson will hold the data in the collection. In mongodump the specific behavior of the output can be changed by the user.
dump directory can use flags like
--out that specify the name of the directory where you want the database dumped. For instance, the name of the dump directory could be
dumbbase instead of dump. The command would look like this.
mongodump --user=uberuser --db=redbase --password=passherd --authenticationDatabase=admin --out=dumbbase
All the collections are dumped into the output folder by default. The name of the folder will be included with the database. The user is able to further control the utility by to only back up one collection at a time. Using the
-collection flag allows the user to say which collection needs to be dumped.
If the only collection called
action_figures should to be dumped, then an example mongodump command would look like:
mongodump --user=ubersuser --db=redbase --password=passherd --authenticationDatabase=admin --out=dumbbase --collection=action_figures
The following folder structure would also be created with the command:
. |_dumbbase |_redbase |_action_figures.metadata.json |_action_figures.bson
Using that command, it's possible to back up one collection at a time, as many times as the user desires. These commands won't overwrite any contents for the output folder.
older collection to the out dump folder would look like:
mongodump --user=uberuser --db=redbase --password=passherd --authenticationDatabase=admin --out=action_figures --collection=older
That command would spit out the
database/redbase folder with the
older.bson files added, making a structure that looks like the following:
. |_action_figures |_redbase |_action_figures.metadata.json |_action_figures.bson |_older.metadata.json |_older.bson
It's also possible to run the backup and have all files in an archive. This is in contrast to dumping everything into a dump directory.
When transferring files between hosts or sending backup files between servers is when this option works best.
It uses the
--archive flag so that the use can specify the name of the archive file. This option creates a single file that can be used to reimport the database with
The user isn't allowed to use both the
--out flags in tandem because of this.
The following mongodump command example below, will dump all databases (collections):
mongodump --db=redbase --username=uberuser --password=passherd --authenticationDatabase=admin --archive=redbase.archive
The opposite of the mongodump utility is the mongorestore, which allows users to restore the database.
The program loads data from the mongodump utility or any binary database dump.
The program differs from mongoimport in that mongorestore will only insert data. The program can't overwrite documents in the database that already exist. This includes updates.
If the id for the document already exists, then the document won't be overwritten. Other wise mongorestore can create a new database or add data to an existing one.
When executing mongorestore, the only requirement is to have the path to the dump directory, the following mongorestore example can be used:
If localhost is used as the host, and the names of the databases created have the same names of the sub-folders in the
dump directory. The command is only slightly more complicated when using a remote host.
The user will have to specify the
--uri flag or include all the standard connection flags like:
--host --db --username --port --password
The program also doesn't require that the entire database get restored. It is possible to restore only a specific collection or list of collections. The user has the option to specify the
--collection flag, the
--db flag and include the path to the BSON file. In this case,
--collection means the name of the collection in the database:
mongorestore --db=newdb --collection=comic_books dump/mydb/product.bson
However, while this command works, it isn't ideal The preferred method to restore different collections is the use the option
This option allows the user to choose a namespace patter to restore the collections for a mongo database.
Here, if the
dump directory dumped databases called
db2, then the final folder structure would look like this:
. |_dump |_db1 |_product.metadata.json |_product.bson |_order.metadata.json |_order.bson |_db2 |_product.metadata.json |_product.bson |_order.metadata.json |_order.bson
By using -nsInclude, the
db1 database could be isolated and imported to restore in the local environment. The command would look like:
mongorestore --db=redbase --nsInclude="db1.*" dump/
Note: The above command would restore all the collections contained in
db1 that was dumped from the database called
redbase. However, the command wouldn't restore anything found in
db2, even though the data is stored in the same dump directory.
Mongodump is a useful tool that helps you back up collections with minimal commands. One command allows the entire collections to be spit out into a single file.
The tool is versatile enough to back up the parts of the database that is needed and comes with a variety of options to change the data you need to save.
SimpleBackups.io is an all-in-one database, website and server snapshot backup automation tool.
It automates MongoDB backups using mongodump to securely dump your MongoDB collections and send them offsite to the cloud for storage.
When you need to ensure your MongoDB backups are secure, you can trust SimpleBackups to take care of it for you.
SimpleBackups will save you a lot of time setting up scripts and ensuring they run without problems. It will alert you when things go wrong, and allows you store your backups on many cloud storage services like Google, DigitalOcean, Wasabi, Dropbox, and more…