Home > Articles > Web Development

Data Administration with MongoDB

  • Print
  • + Share This
Jesse Smith shows you how to administer a MongoDB database by using scripts, security, data maintenance, backups, and more.
Like this article? We recommend

This article shows you how to do some important administrative tasks with MongoDB. Although data administration has always been important, developers often do not experience ways to administer databases.

Database administrators are more adept at doing daily backups, monitoring data usage, importing and exporting data, converting data, performing security tasks, scheduling, and more. As you will see, most of the things you do for relational databases still apply to document storage databases such as MongoDB.

Data Backup and Restores

One of the most common administrative tasks is backing up data, either incrementally or with full backups on certain days of the week. With MongoDB, there are multiple ways to back up your database.

The first method is to back up your data files. I used to use Microsoft SQL Server, which was a very fast and convenient way to back up the data. However, doing a backup this way didn’t properly carry over stored procedures and their permissions.

I don’t know about any problems with MongoDB when it backs up data using data files. To do a backup in this manner, you first must stop the MongoDB database to prevent current writes to the database from incorrectly putting the files in an invalid state.

You can find the data files for a database in the \data\db directory you specified when setting up MongoDB. For example, every MongoDB installation comes with a database called test.

To back up the test database using the data files, simply copy the test.0 and test.ns files. To restore the database, put the files back or overwrite what is there for the test database in a new installation. You then have your original test database after restarting MongoDB and the Mongo shell.

Another way to back up a database is to use the mongodump command, which can modify the data files while the database is running so you do not have to stop the database when using it.

Likewise, to restore a database, use a command called mongorestore. When using just the mongodump command with no options from the bin directory of the MongoDB installation, it backs up all databases to a default directory called dump in the current directory (bin in this case). For this to work, you have to be sure that the process the MonoDB daemon or service is running has write permissions.

To specify only a database, use the –db option like this:

mongodump –db myTest

This command creates a directory called myTest in the dump directory to store the data files. Other variations of the command are self-explanatory:

mongodump  --out /data/backup/
mongodump  --db myTest --out /data/backup/
mongodump --host mongodb.example.net --port 27017

To restore data, use the mongorestore command like this:


This command restores all databases from the default dump directory that was initially created by the mongodump command without any options.

The following variation of the command is self-explanatory:

mongorestore  --dbpath /data/backup/myTest/
  • + Share This
  • 🔖 Save To Your Account