If you’ve backed up a database using the mongodump
utility, you can restore it using the mongorestore
utility.
The mongorestore
utility loads data from either a binary database dump created by mongodump
or the standard input into a mongod
or mongos
instance.
Check for MongoDB Database Tools
The mongorestore
utility is part of the MongoDB Database Tools package. The MongoDB Database Tools are a suite of command-line utilities for working with MongoDB.
If you’re not sure whether you have the MongoDB Database Tools/mongorestore
installed. Try running the following command in your Terminal or Command Prompt to check:
mongorestore --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 mongorestore
commands from your system’s command line (e.g. a new Terminal or Command Prompt window).
Don’t run them from the mongo
shell.
Restore All Databases from a Directory
The following command restores all databases that have been backed up to the dump/
directory:
mongorestore dump/
This example restores the databases to the local instance running on the default port 27017. We know this, because we didn’t provide any host, port, authentication information, etc.
Restore a Specific Database
You can use the --nsInclude
parameter to specify a database to restore.
Example:
mongorestore --nsInclude="PetHotel.*" dump/
In this case, we restored the PetHotel
database. We restored all collections, because we used an asterisk wildcard (*
) to specify all collections.
Restore a Specific Collection
You can also use the --nsInclude
parameter to specify a collection to restore.
Example:
mongorestore --nsInclude="PetHotel.pets" dump/
This example restores the pets collection from the PetHotel
database. If the database doesn’t exist, it is created with a single collection (pets
).
This example is almost identical to the previous example, except that instead of using the asterisk wildcard (*
) to specify all collections, we explicitly specified the collection that we want to restore.
Rename a Collection
You can use the --nsFrom
and --nsTo
parameters to specify a new name for the collection.
Example:
mongorestore --nsFrom='PetHotel.pets' --nsTo='PetHotel.pets2' dump/
This renames the pets
collection to pets2
.
Note that the above code also restores all other databases and collections in the dump/
directory. The only difference is that the pets
collection is restored as pets2
.
If you only want to restore a single collection (and rename it in the process), then use the --nsInclude
parameter.
Example:
mongorestore --nsFrom='PetHotel.pets' --nsTo='PetHotel.pets2' --nsInclude="PetHotel.pets" dump/
Rename a Database
You can use the same concept to rename a database. Simply use the --nsFrom
and --nsTo
parameters to specify a new name for the database.
Example:
mongorestore --nsFrom='PetHotel.pets' --nsTo='PetHouse.pets' --nsInclude="PetHouse.*" dump/
In this case I renamed the PetHotel
database to PetHouse
.
Note that the --nsInclude
parameter specifies the new database name. Also, I use the asterisk wildcard (*
) in order to restore all collections in that database.
Excluding Collections
You can use the --nsExclude
parameter to specify a collection to exclude from the restore process.
Example:
mongorestore --nsInclude="PetHotel.*" --excludeCollection="dogs" dump/
That example restores all collections in the PetHotel
database except for the dogs
collection.
You can use --nsExclude
multiple times to exclude multiple collections from the restore process.
Example:
mongorestore --nsInclude="PetHotel.*" --excludeCollection="dogs" --excludeCollection="employees" dump/
Access Control/Authentication
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
:
mongorestore --host=myhost.example.com --port=37017 --username=homer --authenticationDatabase=admin /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.
Restore from Compressed Files
You can use the --gzip
parameter to restore from compressed files or data stream created by mongodump --gzip
.
Example:
mongorestore --gzip --nsInclude="krankykranes.*" dump3/
Quiet Mode
You can use the --quiet
parameter to limit the output in your Terminal or Command Prompt window.
mongorestore --quiet
Without using this, you’ll probably see a big list of views, collections, etc that are restored.
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.
mongorestore --verbose
You can increase the verbosity by repeating the -v
form multiple times.
Example:
mongorestore -vvvv
More Information about mongodump
The mongorestore
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 mongorestore
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 and restoring your MongoDB databases.