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
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.
- Use the below command to create a database and collections and insert data into the collections.
- Verify whether the database and collection were created successfully or not using the below commands.
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”
- Before installing command line tools, the MongoDB folder structure looks like the screen print below. It will show only the Server folder.
- 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.
- After installing command line tools, the MongoDB folder structure looks like the screen print below. It will show the Server folder and tools folder.
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
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.
- Now let’s try to drop and backup the database.
Drop database command: db.dropDatabase()
Restore database command: mongorestore
Let’s check the database and collection and data inside collection
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.