Introduction:

As for Redis , MongoDB has its own backup utility to export and import data.This tool allow you in a single command to backup your database and your collections and then to load them.

To save your data, we will see how to use mongodump and then for restoration we will use mongorestore.Note that we can also use mongoimport and mongoexport but we will see the difference on this post.

For this topic we will check how to use the commands and their role,there a may we to use it and many arguments to add in the command ( in this blog I will not use the authentication argument but of course it is a best practice to use it , the aim is to show you how to use the backup utility)

MongoDB backup utility

First of all ,to be able to use the backup utility you must have downloaded the MongoDB tools.

Important:

Run all the backup commands from the system command line, not the mongo shell.

What is mongodump

mongodump is a utility that creates a binary export of a database’s contents.

mongodump can export data from:

  • Standalone deployments
  • Replica sets
  • Sharded clusters

mongodump can connect to mongod and mongos instances.

The format of the dump file is in BSON ( binary file ) it has the advantage to take less space than a JSON export

BSON file are not human readable whereas JSON file is.

I advise you to use mongodump over mongimport if you need performance and the data in JSON format are suitable to transferred between heterogeneous systems,

What is 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.it will generate mainly a BSON file to be restored and a metadata file in JSON format

What is mongoimport

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

What is mongoexport

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance.

The mongodump command

Syntax:

mongodump --uri="mongodb://mongodb0.example.com:27017" [additional options]
mongodump --user=your_username --db=your_db_name --password=your_password --authenticationDatabase=admin --out=the_path_where_your_DB_will_be_exported --collection=collection_name

Example:

C:\Program Files\MongoDB\Tools\100\bin>mongodump ( you can add many args )

if you let the single mongodump command it will dump ALL the DB and store it in dump folder

You can also dump a specific collection by using the specific argument

C:\Program Files\MongoDB\Tools\100\bin>mongodump --db=admin --collection=system.users --out=data/
2023-06-22T10:12:53.772+0200 writing admin.system.users to data\admin\system.users.bson
2023-06-22T10:12:53.814+0200 done dumping admin.system.users (2 documents)

Another example to dump the collection TOP100PLAYERS from the DB ANOTHER_NEW_DATABASE and to store it in the data folder

C:\Program Files\MongoDB\Tools\100\bin>mongodump --db=ANOTHER_NEW_DATABASE --collection=TOP100PLAYERS --out=data/

You see in the folder the BSON file and the metadata.json file

The mongrestore command

Syntax:

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

mongorestore --db= < new_db_name > --collection= < collection_name > data/ < db_name > / < collection_name > .bson

Example:

C:\Program Files\MongoDB\Tools\100\bin\data>mongorestore --db=ANOTHER_NEW_DATABASE --collection=TOP100PLAYERS data/ANOTHER_NEW_DATABASE/TOP100PLAYERS.bson --drop

Note:

The –drop arg which will avoid any issue if there is a database overwriting

Data loss simulation

Make a backup before any change ( mongodump) , after that let’s remove an entry from a collection and see if we can restore it

As you see you have 3 Barcelona legends , unfortunately I am still not as the 2 others top players level so they decide to remove my name.

Reminder

To remove a entry from a collection :

From the DB you want to remove the entry:

db.collection_name.deleteOne ( filter, elements of the collection )

db.collection.deleteOne(   <filter>,   {      writeConcern: <document>,      collation: <document>,      hint: <document|string>        // Available starting in MongoDB 4.4   })

Example:

db.TOP100PLAYERS.deleteOne( { "Name":"Nabil" } )

Note:

The command to add entry is

db.getCollection('collection_name').insertOne({"key1":"value","key2":"value"});

db.getCollection('TOP100PLAYERS').insertOne({"Name":"Nabil","Club":"FC Barcelona"});

Restore the data

Now,after watching some of my youtube football skills they decide to give me back my place in this trio , so let’s restore the collection!

C:\Program Files\MongoDB\Tools\100\bin>mongorestore --db=ANOTHER_NEW_DATABASE --collection=TOP100PLAYERS data/ANOTHER_NEW_DATABASE/TOP100PLAYERS.bson --drop

  • Let’s see now if the name is restored in the collection

So as we see the entry is now restored and have the same object ID as expected 🙂

How to add or move a collection to another database

As a bonus ,There is a useful command which allow you to copy or move a collection to another database

db.runCommand({renameCollection:"db_name.collection_name",to:"other_db_name.collection_name"})

warning:

You have to send this command from the admin database otherwise it will through an error as on the screenshot below

Example

db.runCommand({renameCollection:"NEW_DB.TOP100PLAYERS",to:”ANOTHER_NEW_DATABASE.TOP100PLAYERS_MOVED_TO_ANOTHERDB”})

Use the admin database to perform it:

That’s it, you moved your collection and can also rename it if you want.

Conclusion

You know now how to use the MongoDB tools mongodump and mongorestore , to perform easily your backup with any lack of performance.We also see few commands that allow you to move your collections on other databases which can be also really useful.

Feel free to check the my other blogs and also the dbi bloggers and share with us if the post was helpful 😀