Last Updated: May 15, 2019
·
80.22K
· sativaware

Restore a MongoDB database(mongorestore)

As a Developer working with MongoDB a regular task I perform is backing up a production/live database and restoring it to my local development machine.

$ mongorestore --drop -d <database-name> <directory-of-dumped-backup>

MongoDB comes with two commands that allow you to quickly backup(mongodump) and restore(mongorestore) to your running MongoDB instances.

The mongorestore command

Once you have created a backup of the database you wish to restore using the mongodump command - the mongorestore command is used to restore the database.

I use the following command to backup a production/live database and restore it to my local development machine - the command assumes the following:

  • The path to the directory containing the backup you made using mongodump

  • An instance of MongoDB running on the machine the command is run on.

$ mongorestore --drop -d <database-name> <directory-of-dumped-backup>

Options used with the above command

--drop

The restoration procedure will drop every collection from the target database before restoring the data from the dumped backup.

It's worth noting that by default the mongorestore command will only perform inserts and not updates when restoring data - any existing data in the target database will be left intact. Using the --drop option will clear any existing collections, effectively performing a full restore.

-d <database-name>

Specify a target database to restore into - if the target database doesn't exist one will be created.


References

MongoDB - leading nosql, document database

mongorestore command - for restoring data

mongodump command - for backups