The Complete mongodump Guide (with examples)

SimpleBackups founder

Laurent Lemaire

Co-founder, SimpleBackups

Last update on December 22, 2023
Originally posted on December 31, 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.

MongoDB Backup

Table of Contents

What is mongodump

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

Paste your MongoDB connection string, choose a storage, backups are done!

Try SimpleBackups Now →

The mongodump command

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 --user, --db, and --password. The user isn't allowed to combine the two into a single command.

How to use mongodump to back up a collection

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:

  • Database name: redbase
  • Username: ubersuser
  • Password: passherd
mongodump --uri="mongodb://uberuser:passherd@localhost:27107/redbase?ssl=false&authSource=admin"

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.

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

  • Use the --authenticationDatabase flag to specify the database that holds the user's credentials.

The standard mongodump process involves dumping the entire database into a single dump
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 ./dump/redbase.

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 options, indexes, and 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.

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

Working with mongodump and collections

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.

Adding the 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.metadata.json and 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

Using mongodump to dump all databases

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 user can specify the name of the archive file. This option creates a single file that can be used to reimport the database with mongorestore.

The user isn't allowed to use both the --archive and --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

How to restore a mongo database

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

mongorestore dump/

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

This option allows the user to choose a namespace pattern to restore the collections for a mongo database.

Here, if the dump directory dumped databases called db1 and 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.

Conclusion

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.



Back to blog

Stop worrying about your backups.
Focus on building amazing things!

Free 7-day trial. No credit card required.

Have a question? Need help getting started?
Get in touch via chat or at [email protected]

Customer support with experts
Security & compliance
Service that you'll love using