How do you backup data from MongoDB

MongoDB is a NoSQL database and like other DBMS’s MongoDB also focuses on providing safe and secure processing of data. The data stored in a computing machine or any database can be lost and there may be several reasons for such occurrence. In such a situation, if you have not created the backup of your data, you may face a huge loss.

MongoDB offers backup support so that you can back up your data and can reuse it if the data is lost. The backup can be performed on all the MongoDB-based databases present on your system or you can target a specific database or a single collection as well.

In this MongoDB tutorial series, we will guide you to create a backup of your database(s)/collection(s) and how you can retrieve the data.

How to create a backup in MongoDB

This section provides several ways to create a backup in MongoDB. Furthermore, this section is broken down into several subsections to compile an informative section.

How to create a backup of all databases

The command used to create a backup in MongoDB is “mongodump“. The following syntax will help you in this regard:

mongodump <options>

To create a backup of all the databases and associated collections, you must execute the following command in your Ubuntu terminal:

You may have noticed that the command is executed in the Ubuntu terminal. It is a universal command that can be executed on several operating systems and is used to create backups in MongoDB.

$ sudo mongodump

Graphical user interface, text, application, chat or text message Description automatically generated

The collections and associated documents present on your MongoDB server are backed up.

How to backup a database in MongoDB

The syntax to create a backup of a database is provided below:

mongodump --db <database-name>

For instance, we want to create a backup for “linuxhint” database. To do so, we have executed the below-stated command in Ubuntu’s terminal:

$ sudo mongodump --db linuxhint

Graphical user interface, text Description automatically generated

It can be observed from the output that, all the collections and documents associated with linuxhint database are successfully backed up after this execution.

How to backup a collection of a database

Sometimes the size of the database is very large and creating a backup for these databases consumes time as well as storage. In such conditions, the administrator will prefer to create a backup of important collections. The mongodump command also allows you to create a backup of a single collection only. To create a backup of a single collection; one must follow the syntax provided below:

mongodump --db <database-name> --collection <collection-name>

Furthermore, the command written below creates the backup of a collection “staff” and this collection belongs to “linuxhint” database:

> mongodump --db linuxhint --collection staff

Text Description automatically generated

As discussed earlier, the default dump address of the MongoDB database and collections is your home directory. However, you can change the backup address by using the “–out” option in “mongodump” command.

mongodump --db linuxhint --out </path/of/location>

For example, we have used the following command to get the backup of “linuxhint” database at our desired address:

> sudo mongodump --db linuxhint --out /adnan/linuxhintdb_backup/

Text Description automatically generated

How to restore backed up databases/collections in MongoDB

Once the backup is created, you must know the way to restore it. In this section, we will demonstrate the way to restore data that includes databases and collections.

The command used to restore the backup has the following syntax:

mongorestore <options>

A single backup command creates backups for all databases. Similarly, a single restore command retrieves all the databases from the backup directory to your MongoDB server. The command stated below restores all the databases at once:

> mongorestore

Text Description automatically generated


Database Management Systems are used to manage the data of an organization and MongoDB is one of the well-known database management systems. The backup phenomenon has a key role in DBMS’s and almost all database administrators update the backup directory regularly. In this descriptive post, we have provided the possible ways to create a backup in MongoDB. Additionally, we have also provided ways to restore the backed-up data. Wrapping up, this guide is equally important for all users of MongoDB. Lastly, it is recommended to create backups of your data, either you are working on databases or any operations related to data management.

About the author

Adnan Shabbir