Understanding the mongodump Command

Table of Contents

The mongodump command

Database backup is a critical step for every database or system administrator. A database backup is a copy of data from the database that allows the database to be reused in the event of a loss event.

When you wish to transfer your database to a different server or convert to MongoDB, you must back up and restore the database. MongoDB includes the mongodump and mongorestore commands for backing up and restoring databases. It is a very easy and powerful solution for performing backup and restore on live systems.

Mongodump is a backup tool that generates a binary export of a database's contents. It is capable of exporting data from standalone, replica set, and shared cluster deployments.

And here's a quick rundown of the commands that we're going to utilize:

  • --host : Specify the hostname or IP address of the database server.
  • --username : Specify the database username.
  • --password : Specify the password of the database user.
  • --port : Specify the port number of the MongoDB instance.
  • --db : Specify the database name you want to backup.
  • --out : Specify the location of the backup path.

You can run the mongodump command from the system command line, not the mongo shell.

This is the general mongodump command structure:


The --uri and suitably structured string or flag options like --user, --db, and --password allow the user to connect to a mongo database. The user is not permitted to combine the two into a single command.

Backing up files with mongodump

Now that we have a grasp on how to make a proper syntax Mongodump may dump a collection named shoes while using localhost with the following command while using a URI format and the following user information:

  • Database name: sneakers
  • Username: sneakerhead
  • Password: aglet
mongodump --uri="mongodb://sneakerhead:aglet@localhost:27107/sneakers?ssl=false&authSource=admin"

Another mongodump command with the normal flags would look like this:

mongodump --user=sneakerhead --db=sneakers --password=aglet --authenticationDatabase=admin

It is also possible to back up a database to an archive file. In contrast to just dumping the files into a directory. These options are intended for moving data across hosts or switching servers. The --archive parameter specifies the name of the archive. The option generates a single file that can be used to reimport the database with mongorestore. If the database name is the same as the database that needs to be dumped, use the --authenticationdatabase flag with the correct name, or if using URI, ensure the authSource part of the string points to the correct database

The normal mongodump method entails dumping the whole database into a single dump directory, which is called dump by default. This working directory will be placed in the working directory from where the command was run. The directory has a sub-folder called after the database. In the previous example, this would be sneakers, so the new structure would be./dump/sneakers. The specific folder will have two separate files for the collection in the database. This contains both a BSON and a JSON file.

Following the same pattern, the <collection>.metadate.json file will include metadata such as options, indexes, and ns to correlate with the collection's namespace. The BSON file contains the <collection>.bson file, which will house the collection's data. The user can customize the particular behavior of the output in mongodump. The dump directory can utilize options like --out, which gives the name of the directory where the database should be dumped. For example, instead of the dump, the dump directory may be called dumbbase. This is how the command would appear.

mongodump --user=sneakerhead --db=sneakers --password=aglet --authenticationDatabase=admin --out=dumbbase

By default, all collections are dumped into the output folder. The folder's name will be saved in the database. The utility may be further customized by just backing up one collection at a time.

The -collection parameter allows the user to specify which collection to be dumped.

If only the skate_shoes collection should be dumped, an example mongodump command would be as follows:

mongodump --user=sneakerhead --db=sneakers --password=aglet --authenticationDatabase=admin --out=dumbbase --collection=action_figures

The following folder structure would also be created with the command:


That command allows the user to back up one collection at a time as many times as they like. These instructions will not replace any files in the output folder.

To add an older collection to the output folder, type:

mongodump --user=sneakerhead --db=sneakers --password=aglet --authenticationDatabase=admin --out=skate_shoes --collection=older

That command would return the database/sneakers folder including older.metadata.json and older.metadata.json.bson files were inserted, resulting in the following structure:


Using mongodump to dump all databases

It is also possible to conduct the backup and archive all files. This differs from dumping everything into a dump directory. This option works well for moving data between hosts or sending backup files between servers.

It employs the —archive parameter to allow the user to define the name of the archive file. This option generates a single file that may be used to reimport the database using mongorestore. Because of this, the user is not permitted to use both the --archive and --out flags concurrently. The following mongodump command example will dump all databases (collections):

mongodump --db=sneakers --username=sneakerhead --password=aglet --authenticationDatabase=admin --archive=sneakers.archive

‍A mongorestore example and how to restore a mongo database

The mongorestore utility is the inverse of the mongodump tool, and it allows users to restore the database. The application loads data from the mongodump utility or any binary database dump. It varies from mongoimport in that mongorestore will only insert data. The software cannot replace existing documents in the database. This includes changes.

Basic syntax of a proper mongorestore command will look like this:

mongorestore --host [host-name] --username [username] --password [password] --port [port-number] --db [database-name] --drop [backup-location]

If the document's id already exists, the document will not be replaced. Otherwise, mongorestore can establish a new database or add data to an existing one. The sole need for running mongorestore is the location of the dump directory; the following mongorestore example can be used:

mongorestore dump/

If localhost is used as the host, and the database names are the same as the subfolders in the dump directory. When utilizing a remote host, the command becomes just slightly more difficult.

The user will have to specify the --uri flag or include all the standard connection flags like:


The application also does not require the restoration of the full database. Only a particular collection or set of collections can be restored. The user has the option of specifying the --collection and --db options, as well as the location of the BSON file. In this scenario, --collection refers to the name of the database collection:

mongorestore --db=newdb --collection=skate_shoes dump/mydb/product.bson

While this command works, it isn't ideal. The preferred approach to restoring different collections is to utilize the option --nsInclude. This option allows the user to choose a namespace pattern to restore the collections for a mongo database.

For instance, if the dump directory dumped databases labeled db1 and db2, the final folder structure would look like this:


The db1 database might be separated and imported to restore in the local environment with --nsInclude. The command would be as follows:

mongorestore --db=sneakers --nsInclude="db1.*" dump/

Note: The above command would restore all the collections contained in db1 that were dumped from the database called sneakers. However, the command wouldn't restore anything found in db2, even though the data is stored in the same dump directory.


Mongodump is a great tool for backing up collections with a few commands.

The complete collection may be split out into a single file with a single command. The program is adaptable enough to back up the sections of the database that are required and comes with a choice of options to adjust the data you need to preserve.

Automated MongoDB Backups

Ottomatik 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 Ottomatik to take care of it and put your worries away.

Ready to secure your backups today?

Try for free
14 Day Free Trial • Cancel Anytime • No Credit Card Required