There are several ways to back up a database in MongoDB. One quick way to back up a database is to use the mongodump tool.
mongodump reads data from a MongoDB database and creates high fidelity BSON files which the mongorestore utitlity can use to restore a MongoDB database.
With mongodump, you can back up a collection, a database, or all databases.
Check for MongoDB Database Tools
mongodump is part of the MongoDB Database Tools package. The MongoDB Database Tools are a suite of command-line utilities for working with MongoDB.
You may or may not have the MongoDB Database Tools/mongodump installed. Try running the following command in your Terminal or Command Prompt to check:
mongodump --version
If you don’t have it, you can use the installation instructions over at the MongoDB website to install it on to your system.
Where to Run the Commands?
You need to run mongodump commands from your system’s command line (e.g. a new Terminal or Command Prompt window).
Don’t run them from the mongo shell.
Back Up All Databases
To back up all databases and collections in the local instance running on the default port 27017, use the mongodump command without any arguments.
mongodump
Running the above code dumps all databases to a folder called dump/.
Note that it excludes the local and config databases.
Also note that mongodump only captures the documents in the database. When you do a restore, mongorestore or mongod must rebuild the indexes after restoring data.
Here’s what the files look like in the dump/ folder.
tree dump
Result:
dump
├── PetHotel
│ ├── employees.bson
│ ├── employees.metadata.json
│ ├── owners.bson
│ ├── owners.metadata.json
│ ├── pets.bson
│ ├── pets.metadata.json
│ ├── pettypes.metadata.json
│ ├── players.bson
│ ├── players.metadata.json
│ ├── scores.bson
│ ├── scores.metadata.json
│ ├── students.bson
│ └── students.metadata.json
├── admin
│ ├── system.users.bson
│ ├── system.users.metadata.json
│ ├── system.version.bson
│ └── system.version.metadata.json
└── krankykranes
├── employees.bson
├── employees.metadata.json
├── products.bson
└── products.metadata.json
Those files are obviously specific to my MongoDB installation, which contains test databases. In my case, 3 databases were dumped: PetHotel, admin, and krankykranes.
Back Up to a Specific Location
You can use --out or -o to specify a location to put the files for the dumped databases.
Example:
mongodump --out=data/backups/
Or:
mongodump -o=data/backups/
In this case, each database folder is output directly into the data/backups/ folder (i.e. there’s no /dump/ folder).
Back Up a Specific Database
You can use the --db parameter to specify a database to back up.
mongodump --db=krankykranes
This dumps the krankykranes database to the default dump/ folder.
Back Up a Specific Collection
You can use --collection or -c to specify a collection to back up.
Example:
mongodump --db=krankykranes --collection=products
Or:
mongodump --db=krankykranes -c=products
This dumps the products collection to the dump/krankykranes folder.
Excluding Collections
You can also use the --excludeCollection parameter to specify a specific collection to exclude from the backup (i.e. not include in the backup).
Example:
mongodump --db=PetHotel --excludeCollection=employees
This dumps all collections from the PetHotel database except for the employees collection.
To exclude more than one collection, use a separate --excludeCollection parameter for each collection that you want to exclude.
Example:
mongodump --db=PetHotel --excludeCollection=employees --excludeCollection=students
That excludes the employees and students collections from the backup.
You can also use the --excludeCollectionsWithPrefix parameter to exclude collections with a given prefix.
mongodump --db=PetHotel --excludeCollectionsWithPrefix=p
That dumps all collections except those that start with the letter p.
The following one excludes all collections that start with pet.
mongodump --db=PetHotel --excludeCollectionsWithPrefix=pet
You can use multiple --excludeCollectionsWithPrefix arguments to exclude collections with multiple prefixes.
Example:
mongodump --db=PetHotel --excludeCollectionsWithPrefix=p --excludeCollectionsWithPrefix=s
Compress the Output
You can use the --gzip parameter to compress the output.
mongodump --gzip --db=krankykranes
This compresses the individual files.
tree dump
Result:
dump
└── krankykranes
├── employees.bson.gz
├── employees.metadata.json.gz
├── products.bson.gz
└── products.metadata.json.gz
We can see that all files have the .gz extension.
When outputting to an archive file or the standard out stream, the --gzip option compresses the archive file or the data output to the stream.
Convert Views to Collections
You can use the --viewsAsCollections parameter to export read-only views as collections.
mongodump --db=PetHotel --viewsAsCollections
When you export a view as a collection, mongodump produces a BSON file containing the documents in the view. If you use mongorestore to restore the produced BSON file, the view will be restored as a collection.
Without using this argument, mongodump exports each view’s metadata. If you include a view’s metadata file in a mongorestore operation, the view is recreated.
Using --viewsAsCollections also omits all standard collections. Therefore, when I run the above code, my dump/ folder structure looks like this:
tree dump
Result:
dump
└── PetHotel
├── pettypes.bson
└── pettypes.metadata.json
In my PetHotel database, I have a view called pettypes. When I export this as a view, only the metadata is exported. But when I convert it to a collection, a pettypes.bson file is created which wouldn’t be there if I had exported the view as a view.
Here’s what happens if I export that view without converting it to a collection.
mongodump --db=PetHotel --collection=pettypes
Then run the tree command, to get the folder structure.
tree dump
Result:
dump
└── PetHotel
└── pettypes.metadata.json
So we can see that just the metadata for the view is dumped.
Quiet Mode
You can use the --quiet parameter to limit the output in your Terminal or Command Prompt window.
mongodump --quiet
Without using this, you’ll probably see a big list of views, collections, etc that are backed up.
Verbose Mode
On the flip side, you can use the --verbose or -v parameters to increase the output in your Terminal or Command Prompt window.
mongodump --verbose
You can increase the verbosity by repeating the -v form multiple times.
Example:
mongodump -vvvv
Running that on my system significantly increased the verbosity.
Backup Query Results
You can use the --query or -q parameters to dump the result of a query.
Example:
mongodump --db=PetHotel --collection=pets --query='{ "type": "Dog" }'
When doing this, surround the query with single quotes. You can use double quotes for the fields.
Archives & Standard Output
You can use the --archive or parameter to write the output to a specified archive file or, if the archive file is unspecified, to the standard output (stdout) stream so that you can pipe to another process.
Example of outputting to an archive file:
mongodump --archive=PetHotel.20201230.archive --db=PetHotel
In the next example, I write to the standard output stream, then pipe to mongorestore:
mongodump --archive --db=PetHotel --collection=pets --query='{ "type": "Dog" }' | mongorestore --archive --nsFrom='PetHotel.pets' --nsTo='PetHotel.dogs'
That results in a new collection called dogs appearing in the PetHotel database (and the collection only contains documents that have a type field with a value of dog).
Access Control
The previous examples were done on the local machine using the default port. This meant that we were able to run mongodump without specifying things like --host, --port, --username, etc.
Here’s an example that uses those parameters to authenticate as homer:
mongodump --host=myhost.example.com --port=37017 --username=homer --authenticationDatabase=admin --out=/backups/mongodump-2020-12-30
We could have also used the --password parameter, but we didn’t. If you pass --user but not --password, you’ll be prompted for the password.
More Information about mongodump
The mongodump utility accepts plenty of other useful parameters, and there are also various factors to consider when using it as part of a backup and recovery strategy.
See the mongodump documentation on the MongoDB website for more info.
Other Options
mongodump and mongorestore are simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems.
See MongoDB Backup Methods on the MongoDB website for other methods for backing up your MongoDB databases.