How To Backup and Restore MongoDB Database

By bhagwatchouhan
How To Backup and Restore MongoDB Database

MongoDB is the most popular NoSQL database server and it can be used to store JSON documents in BSON format. MongoDB is a document-oriented, open-source, and platform-independent Database Management System (DBMS). This tutorial provides the steps required to take backup and restore the MongoDB database in BSON format. It also provides the steps required to import or export the database in JSON format.

Notes: You can also follow How To Install MongoDB 5 on Windows, How To Install MongoDB 5 on Ubuntu 20.04 LTS, and How To Install MongoDB 5 on Mac.

 

Import or Export in JSON Format

This section provides the steps to import or export the MongoDB database in JSON format. MongoDB internally stores the data in BSON format. It assumes that you have already installed the MongoDB Database Tools since the commands mongoexport and mongoimport are not part of the MongoDB database installation.

mongoexport

We can export the database in JSON or CSV format using the mongoexport command as shown below.

Syntax

mongoexport --collection=<coll> <options> <connection-string>

Examples

# Export the collection of the given database to the given file
mongoexport --collection=employees --db=organization --out=employees.json
# Export using URI Option mongoexport --uri="mongodb://db.example.com:27017/organization" --collection=employees --out=employees.json
# Export using URI Option - With authentication mongoexport --uri="mongodb://db.example.com:27017/organization" --collection=employees --out=employees.json -u "username" -p "password" --authenticationDatabase "admin"
mongoexport --uri="mongodb://<username>:<password>@db.example.com:27017/organization" --collection=employees --out=employees.json --authenticationDatabase "admin"
# Export using Host Option mongoexport --host="db.example.com:27017" --collection=employees --db=organization --out=employees.json
# Export using Host and Port Options mongoexport --host="db.example.com" --port=27017 --collection=employees --db=organization --out=employees.json

mongoimport

We can import the database in JSON or CSV format using the mongoimport command as shown below. We need to make sure that the existing collection does not have documents with the same identity, else it will throw duplicate key error as shown below. If we are sure while importing the collection, we can drop the existing collection by providing the option --drop.

# Duplicate key error example
continuing through error: E11000 duplicate key error collection: organization.employees index: _id_ dup key: { _id: ObjectId('617e7a58b123271ed1b3158d') }

Syntax

mongoimport <options> <connection-string> <file>

Examples

# Import the collection to existing collection to the given database from the given file
mongoimport --db=organization --collection=employees employees.json
# Import the collection by dropping existing collection to the given database from the given file mongoimport --db=organization --collection=employees --drop employees.json
# Import with authentication mongoimport --db=organization --collection=employees --drop employees.json -u "username" -p "password" --authenticationDatabase "admin"

 

Import or Export in BSON Format

This section provides the steps to backup or restore the MongoDB database in BSON format. MongoDB internally stores the data in BSON format. It assumes that you have already installed the MongoDB Database Tools since the commands mongodump and mongorestore are not part of the MongoDB database installation.

mongodump

We can export the database in BSON format using the mongodump command as shown below.

Syntax

mongodump <options> <connection-string>

Examples

# Export database using URI to the given directory
mongodump --uri="mongodb://db.example.com:27017/organization" --out=db
# Export using URI Option - With authentication mongodump --uri="mongodb://db.example.com:27017/organization" --out=db -u "username" -p "password" --authenticationDatabase "admin"
# Export collection using URI to the given directory mongodump --uri="mongodb://db.example.com:27017/organization" --collection=employees --out=db
# Export using Host Option mongodump --host="db.example.com:27017" --collection=employees --db=organization --out=db
# Export using Host and Port Options mongodump --host="db.example.com" --port=27017 --collection=employees --db=organization --out=db

mongorestore

We can import the database in BSON format using the mongorestore command as shown below. We need to make sure that the existing collection does not have documents with the same identity, else it will throw the duplicate key error. If we are sure while importing the collection, we can drop the existing collection by providing the option --drop.

Syntax

mongorestore <options> <connection-string> <directory or file to restore>

Examples

# Import using URI
mongorestore --uri="mongodb://db.example.com:27017/organization" db/organization
# Import using URI, drop existing collections
mongorestore --uri="mongodb://db.example.com:27017/organization" --drop db/organization
# Import using URI with Authentication mongorestore --uri="mongodb://db.example.com:27017/organization" --drop db/organization -u "username" -p "password" --authenticationDatabase "admin"

 

Summary

This tutorial provided the syntax with examples to import or export the MongoDB database in both JSON and BSON formats.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan