How to Back Up and Restore a MongoDB Database?
Last Updated :
20 Feb, 2025
MongoDB is considered one of the classic examples of NoSQL systems. Its documents are made up of key-value pairs, which are the basic unit of data in MongoDB. Whether we're dealing with accidental data loss, hardware failures, or other unforeseen issues, having a solid backup and restoration plan can save our significant time and resources.
In this article, we will learn the process of backing up and restoring MongoDB databases, using practical examples of how to back up entire databases or specific collections, both with and without compression. We’ll cover the process in the context of a MongoDB setup on Ubuntu 20.04.2 LTS.
Understanding MongoDB’s Data Format
MongoDB stores its data using a format called BSON (Binary JSON), which is similar to the widely-used JSON (JavaScript Object Notation) format. BSON offers enhanced performance by enabling faster data processing and searching.
While JSON is human-readable, BSON is more efficient for storing large amounts of data, making it a suitable format for MongoDB's backend storage. Although BSON takes up slightly more space than JSON, the improved speed compensates for this disadvantage.
Setting Up MongoDB Databases and Collections
Before we get into backing up, let's first create some databases and collections to work with. Here’s a simple example. So, let’s connect to our MongoDB and create three databases for backups. They will be DB1, DB2, and DB3. To fill these databases, let’s add some collections.
use DB1
db.createCollection("posts1")
db.createCollection("address1")
db.createCollection("phone1")
use DB2
db.createCollection("posts2")
db.createCollection("address2")
db.createCollection("phone2")
use DB3
db.createCollection("posts3")
db.createCollection("address3")
db.createCollection("phone3")
Output:

After creating the databases and collections, you can check them with the following commands:
use admin
show dbs
Output:

As you can see, our databases are okay. Now let’s check if they have the collections that we have created before.
use DB1
show collections
use DB2
show collections
use DB3
show collections
Output:

All the collections are there. Now we can proceed to the first backup option.
1. Backing Up a Database with Compression
Let’s start by creating a compressed backup of a specific database (DB1
) using the mongodump utility. First, create a directory to store your backup:
$ mkdir /tmp/backup_v1
Now use the following command to create a backup:
$ mongodump --host=localhost --gzip --db DB1 --archive=/tmp/backup_v1/backup-db-1.gz
Now, after creating a backup, let’s move to the database directory and see what it has using the following command:
$ ls -la /tmp/backup_v1
Output:

As you can see, our Backup Copy has just been created.
Now, restore data from this kind of backup we use the following command:
$ mongorestore --gzip --archive=/tmp/backup_v1/backup-db-1.gz
In this example, we use only one command to make a backup copy, which is then archived. There are several options for how to make a backup copy.
2. Back up all databases, without data compression
If you prefer to back up all databases without compression, here’s how you can do it.
Create a Directory for the Backup:
$ mkdir /tmp/backup_v2
Run the Backup Command:
$ mongodump --out /tmp/backup_v2
Output:

After successful backup, let’s move to the database directory and see what it has. It has BSON and JSON collections without compression.
$ ls -la /tmp/backup_v2/db1
Output:

Now restore data from this backup using the following command:
$ mongorestore --drop --dir /tmp/backup_v2
Here, the --drop parameter is used to drop a collection before importing (if it exists) to avoid duplicate key errors. This --drop parameter should be used with caution.
Restoring Specific Collections:
Now restore a specific collection (for example, posts1 from DB1) from a backup of all databases:
$ mongorestore --drop --dir /tmp/backup_v2 --nsInclude 'DB1.posts1'
Restore all databases and all collections except for a specific collection (for example, posts1 from DB1):
$ mongorestore --drop --dir /tmp/backup_v2 --nsExclude 'DB1.posts1'
Back up all databases with compression
To back up all databases with gzip compression, use the following steps:
Create a Backup Directory:
$ mkdir /tmp/backup_v3
Run the Command with Compression:
mongodump --gzip --out /tmp/backup_v3

Restore from the Compressed Backup:
$ ls -la /tmp/backup_v3/db1
Here the backup file contains compress files.

Now restore data from this kind of backup:
$ mongorestore --gzip --drop --dir /tmp/backup_v3
Back up all databases with compression into a single archive (.gz)
To create a backup of all the databases with compression into a single archive (.gz):
Create a Backup Directory:
$ mkdir /tmp/backup_v4
Run the Backup Command with Archive:
$ mongodump --gzip --archive=/tmp/backup_v4/my_backup.gz

Now restore data from the backup using the following command:
$ mongorestore --gzip --drop --archive=/tmp/backup_v4/my_backup.gz
Back up a specific database
Create a backup of a specific database
$ mkdir /tmp/backup_v5
$ mongodump --gzip --out /tmp/backup_v5 --db DB2

Restore data from this backup:
$ mongorestore --gzip --drop --dir /tmp/backup_v5
Back up a single address2 collection from DB2
Create a backup a single address2 collection from DB2
$ mkdir /tmp/backup_v6
$ mongodump --gzip --out /tmp/backup_v6 --db DB2 -c address2

Back up the entire DB2 except for a single posts2 collection
Create a back up the entire DB2 except for a single posts2 collection
$ mkdir /tmp/backup_v7
$ mongodump --gzip --out /tmp/backup_v7 --db DB2 --excludeCollection posts2

Conclusion
Backing up and restoring MongoDB data is an essential part of any database management strategy. MongoDB’s mongodump and mongorestore utilities make this process efficient and flexible, whether we are backing up a single collection, an entire database, or even applying compression for more efficient storage. By following the steps outlined in this guide, we can ensure that our MongoDB data is properly safeguarded, enabling fast recovery in the event of data loss or corruption.
Similar Reads
How to Rename a MongoDB Database?
Renaming a MongoDB database may be necessary for various reasons such as reorganization or clarity. MongoDB provides several methods for renaming a database including using shell commands, dumping and restoring data or directly manipulating database files.In this article, we will learn different app
4 min read
How to Connect Node to a MongoDB Database ?
Connecting Node.js to MongoDB is a common task for backend developers working with NoSQL databases. MongoDB is a powerful, flexible, and scalable database that stores data in a JSON-like format. In this step-by-step guide, we'll walk through the entire process from setting up your development enviro
6 min read
How to Connect to a MongoDB Database Using Node.js
MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ
4 min read
How to Secure the MongoDB Database
In todayâs digital era, securing databases is more critical than ever, especially for organizations storing sensitive user and business data. MongoDB, a widely used NoSQL database, requires robust security measures to prevent unauthorized access, data breaches, and cyber threats.By default, MongoDB
10 min read
How to Create a MongoDB Dump of Database
MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. However, to protect our data from potential data loss or corruption, itâs critical to have a reliable MongoDB backup strategy in place. In this article, we will go through the process of creating a MongoDB d
7 min read
How to Dump and Restore PostgreSQL Database?
PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
How to use MongoDB atlas to save data ?
As modern applications require a database for storing data, MongoDB is a great choice. MongoDB Atlas is the global cloud database service for modern applications. We can use it for storing our application data on its server. Follows these steps for how to use MongoDB Atlas for saving data:Step 1: Vi
3 min read
MongoDB - Backup and Restoration
MongoDB, a leading NoSQL database, is known for its flexibility, scalability, and ease of use. However, like any database, MongoDB is susceptible to data loss due to hardware failures, software issues, human errors, or even cyberattacks. Database backup and restore processes are critical for maintai
6 min read
How to create new Mongodb database using Node.js ?
mongodb module: This Module is used to performing CRUD(Create Read Update Read) Operations in MongoDb using Node.js. We cannot make a database only. We have to make a new Collection to see the database. The connect() method is used for connecting the MongoDb server with the Node.js project. Please r
1 min read
How to Seed a MongoDB Database Using Docker Compose
Seeding a MongoDB database is a common task in many development and testing scenarios. It involves populating the database with initial data to ensure consistent and predictable behavior during the application development and testing phases. Docker Compose is a powerful tool that simplifies the proc
4 min read