How to Backup/Restore MongoDB Database and Collections

Basics of MongoDB

MongoDB is a popular, open-source NoSQL database management system. It's designed to handle large volumes of unstructured or semi-structured data, making it well-suited for modern applications that require flexible data models and scalable architecture

The MongoDB Database Tools

The MongoDB Database Tools are a collection of command-line utilities for working with a MongoDB deployment.

Backing up and restoring a MongoDB database and collection involves using the built-in utilities provided by MongoDB or third-party tools. Here's how you can do it using MongoDB's utilities:

Binary Import / Export
 

mongodump Creates a binary export of the contents of a mongodb database.
mongorestore Restores data from a mongodump database dump into a mongodb or mongos
bsondump Converts BSON dump files into JSON.


Let’s try with an example to backup and restore

Create Sample Database

  • The first step is to create a Mongo DB Database. For that, we can use Mongosh provided in MongoDB Compass. Another way we can use a command prompt. We will use mongoSH for this article.Create Sample mongodb Database
  • Use the below command to create a database and collections and insert data into the collections.
    Create Sample mongodb Database
  • Verify whether the database and collection were created successfully or not using the below commands.
    Create Sample mongodb Database

Create Backup Directory

  • To store the backup of the MongoDB database we need to create a directory. Let’s create a directory “Backup” in c drive. It can also be created in d or other respective drives with any other name. For this article, we will create a Backup directory in C Drive.

Setup the MongoDB Database Tools Commands

  • MongoDB tools command directly not support. We need to install the MongoDB Command Line Database Tools.
  • Let’s check what happens if we try to backup the database without installing command line tools. Let’s try to backup the database with mongodump command.
     It’s throwing error “mongodump is not defined”
    Setup the MongoDB Database Tools Commands
  • Before installing command line tools, the MongoDB folder structure looks like the screen print below. It will show only the Server folder.Setup the MongoDB Database Tools Commands
  • Let’s install command line tools. To install command line tools we can get them from the given link. https://www.mongodb.com/try/download/database-tools.Setup the MongoDB Database Tools Commands
  • After installing command line tools, the MongoDB folder structure looks like the screen print below. It will show the Server folder and tools folder.Setup the MongoDB Database Tools Commands

Backup and Restore Database

  • Once installed the command line tools need to map the backup folder with the command line tools. Let’s execute mongodump command to Backup the database. Before executing command we need to set the path otherwise it will throw the error ” 'mongodump' is not recognized as an internal or external command, operable program or batch file”
    Another way we can set MongoDB's bin directory is in your system's PATH
    Backup and Restore mongodb Database
    The above screen print shows how to map the command line tools path to activate Mongodump command.
  • Let’s check the backup directory to verify the backup database files.
    Backup and Restore mongodb Database
  • Now let’s try to drop and backup the database.
    Drop database command: db.dropDatabase()
    Backup and Restore mongodb Database
    Restore database command: mongorestore
    Backup and Restore mongodb Database
    Let’s check the database and collection and data inside collection
    Backup and Restore mongodb Database

Backup and Restore Database Collection

  • Let’s create another backup folder called “Backup_Collections”.
    Backup and Restore Database Collection
  • Let’s run the command to backup the collection Orders. below is the command to backup the collection.
    mongodump --db your_Database_Name --collection Your_Collection_Name
    Backup and Restore Database Collection
    Directory View
    Backup and Restore Database Collection
  • Let’s drop the Order collection and try to restore from the backup using the restore command
    drop collection command:
    db.yourcollectionName.drop()
    Backup and Restore Database Collection
    Restore collection from the backup. Below is the command to restore the collection.
    mongorestore --db your_Database_Name --collection Your_Collection_Name BSON Filename with full path
    In our example, we will use the command as mentioned below.
    mongorestore --db mongodb_backup_restore --collection orders C:\Backup_Collections\dump\mongodb_backup_restore\orders.bson
    Backup and Restore Database Collection

MongoDB Command line tools pre-requisite

Starting with MongoDB 4.4, the MongoDB Database Tools are released separately from the MongoDB Server and use their versioning, with an initial version of 100.0.0. Previously, these tools were released alongside the MongoDB Server and used matching versioning.


Similar Articles