MongoDB - Create Backup


Dump MongoDB Data

To create backup of database in mongodb you should use mongodump command. This command will dump all data of your server into dump directory. There are many options available by which you can limit the amount of data or create backup of your remote server.


Basic syntax of mongodump command is as follows



Start your mongod server. Assuming that your mongod server is running on localhost and port 27017. Now open a command prompt and go to bin directory of your mongodb instance and type the command mongodump

Consider the mycol collection has following data.


The command will connect to the server running at and port 27017 and back all data of the server to directory /bin/dump/. Output of the command is shown below:

DB Stats

There are a list of available options that can be used with the mongodump command.

This command will backup only specified database at specified path
mongodump --host HOST_NAME --port PORT_NUMBERThis commmand will backup all databases of specified mongod instance.mongodump --host --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORYmongodump --dbpath /data/db/ --out /data/backup/
mongodump --collection COLLECTION --db DB_NAMEThis command will backup only specified collection of specified database.mongodump --collection mycol --db test

Restore data

To restore backup data mongodb's mongorestore command is used. This command restore all of the data from the back up directory.


Basic syntax of mongorestore command is


Output of the command is shown below:

DB Stats