Data back up and restore in MongoDB

Foridul Islam
3 min readAug 6, 2022

--

A complete guide to backing up and restoring data in the MongoDB database

The Backup and Restore in MongoDB is an important part to handle a database. MongoDB is one of the most popular NoSQL databases. It’s becoming popular because it allows t store data without worrying about the data structure or number of fields. Moreover, the MongoDB documents are looks like JSON objects and it’s very familiar to users.

MongoDB back up

MongoDB can create a backup for an entire server, database, or collection, or we can use a query to backup a part of a collection.

There has basically 2 methods to backup data.

(i) mongodump

(ii) mongoexport

(i) Data back up using mongodump

mongodump is a utility for creating a binary export of the contents of a database. mongodump can export data from either mongod or mongos instances; i.e. can export data from standalone, replica set, and sharded cluster deployments.

mongodump is a utility for creating a binary export of the contents of a database. It generates binary copies of data. It creates better and more efficient backups.

Direct back using mongodump

To back data directly you can run the following command

Mongodump --out /opt/backup/mongodump-2022-08-06

Back up from a remote MongoDB instance

To backup data from a remote MongoDB instance, run the following command.

mongodump --host 172.16.0.168 --port 27017 --out /opt/backup/mongodump-2022-08-06

Backup from a secure MongoDB instance

To back up data from a secure MongoDB instance, run the following command.

mongodump --host mongodb1.example.net --port 37017 --username user --password pass --out /opt/backup/mongodump-2022-08-06

Backup a whole database

To backup a whole database, run the following command.

mongodump --host 172.16.0.168 --port 27017 --out --db "myDB" --out /opt/backup/mongodump-2022-08-06

Backup a single collection of a database

To backup, a single collection or table of databases, run the following command.

mongodump --host 172.16.0.168 --port 27017 --out --db "myDB" --collection Persons --out /opt/backup/mongodump-2022-08-06

Here, myDB is the database name.

(ii) Data backup using mongoexport

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. The mongoexport has more options and granularity on which fields within each document record to export.

To back up a single collection or table of a database, run the following command.

mongoexport --host 172.16.0.168:27017 --db myDB --collection Persons --out c:\backupdata\168\Persons.json

Restore database

Apart from excellent backups, MongoDB also provides you the facility to restore your data.

There has basically 2 methods to restore data.

(i) mongorestore

(ii) mongoimport

(i) Restore data using mongorestore

The mongorestore program loads data from either a binary database dump created by mongodump or the standard input into a mongod or mongos instance.

Restore a while database

To restore a whole database, run the following command.

mongorestore --host 172.16.0.168 --port 27017  --db "myDB" "D:\backupdata\168\myDB"

Restore a single collection of a database

To restore a single collection or table of a database, run the following command.

mongorestore --host 172.16.0.168 --port 27017  --db "myDB" --collection Persons "D:\backupdata\168\myDB\Persons.bson"

(ii) Restore data using mongoimport

The mongoimport command imports content from an extended JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.

The big difference between mongorestore and the mongoimport command is the mode flag which has the options of: insert, upsert, delete, and merge.

To restore or import a json collection, run the following command.

mongoimport --host 172.16.0.168:27017 --db myDB --collection Persons --file c:\backupdata\168\Persons.json

Sometimes, to avoid error you can add forceTableScan within your command like following.

mongoimport --forceTableScan --host 172.16.0.168:27017 --db myDB --collection Persons --file c:\backupdata\168\Persons.json

Summary

These mongodump and mongostore command is a very useful commands to back and restore data in MongoDB. The mongoexport and mongoimport commands are great for working with collections within a MongoDB database and inserting or updating documents within those collections.

Thanks for reading!

--

--

Foridul Islam
Foridul Islam

Written by Foridul Islam

Sr. Software Engineer at SELISE Digital Platforms

No responses yet