How to Create a MongoDB Dump of a Database

Tahseen Tauseef Feb 02, 2024
  1. the mongodump Command in MongoDB
  2. Use the mongodump Command to Create a MongoDB Dump of a Database
  3. Use the mongodump Command to Create a MongoDB Dump of All Databases
  4. Use the mongorestore Command to Restore a Mongo Database
  5. Conclusion
How to Create a MongoDB Dump of a Database

In this MongoDB tutorial, you will be given a walk-through of Mongodump and Mongorestore, how to use them, and some simple examples to back up and restore your collections with both tools.

the mongodump Command in MongoDB

Mongodump is a tool that creates a binary export from the contents of a database. mongod and mongos instances are supported by this tool.

Users may export data from standalone, replica, set, and sharded cluster installations using Mongodump.

Mongodump used to be updated regularly, with new versions provided whenever the MongoDB Server was upgraded. The utility, however, has had its versioning since MongoDB 4.4. MongoDB 3.6, 4.0, 4.2, and 4.4 are supported in the current version, 100.2.1.

Even though all previous versions of MongoDB are supported, Mongodump may not be fully compatible with them. The gadget acts as a backup plan.

This is one approach for IT professionals to back up and restore databases daily if they want to plan backups (collections). For example, Mongodump can save everything in a single file, and Mongorestore may be used to restore the database entirely later.

We may execute the mongodump command from the system command line rather than the mongo shell. The mongodump command structure is as follows:

mongodump <options> <connection-string>

The -uri and properly structured string or flag options like -user, -db and -password can be used to connect to a mongo database. Unfortunately, the user can’t merge the two different commands into one.

Use the mongodump Command to Create a MongoDB Dump of a Database

mongodump can dump a collection called database1 using the localhost with the following command, using a URI format and the user information.

  1. Database name: database1
  2. Username: user123
  3. Password: mydatabse123
mongodump --uri="mongodb://uberuser:mydatabse123@localhost:27107/database1?ssl=false&authSource=admin"

Another example with the mongodump command using the standard flags would look like this:

mongodump --user=uberuser --db=database1 --password=mydatabse123 --authenticationDatabase=admin

The database backup can also be saved as an archive file. In comparison, dumping the files into a directory is not good.

These choices are for switching servers or transferring data across hosts. The -archive option allows the user to provide the archive’s name.

This option generates a single file that can reimport the database using Mongorestore.

If the database’s name is the same as the database to be dumped, use the -authenticationDatabase flag with the correct name. When using URI, ensure the authSource component links to the proper database.

The typical mongodump method dumps the whole database into a single dump directory, which is by default named dump. This directory will be created in the same directory where we ran the command.

The database will be named after a sub-folder in the directory. This would be database1 in the previous example; thus, the new structure would be ./dump/database1.

There will be two separate files for the database collection in the appropriate folder - a BSON and a JSON file.

The .metadate.json file will follow a similar structure, containing metadata such as options, indexes, and ns to match the collection’s namespace. The .bson file in the BSON file will retain the collection’s data.

The user can customize the specific behavior of the output in the mongodump.

Flags like -out can be used in the dump directory to provide the directory’s name where the database should be dumped. For example, instead of dump, the dump directory may be called dumbbase.

Below is how the command would appear.

mongodump --user=user123 --db=database1 --password=mydatabse123 --authenticationDatabase=admin --out=dumbbase

By default, all collections are dumped into the output folder. The folder’s name will be added to the database.

The user can further restrict the utility’s functionality by only backing up one collection at a time. The user can specify which collection to be dumped by using the -collection parameter.

If only the action figures collection is to be dumped, then an example mongodump command would be:

mongodump --user=user123 --db=database1 --password=mydatabse123 --authenticationDatabase=admin --out=dumbbase --collection=action_figures

We can also create the following folder structure with the command:

.
 |_dumbbase
 |_database1
 |_action_figures.metadata.json
 |_action_figures.bson

It is possible to back up one collection at a time with that command as many times as the user wants. These instructions will not overwrite the output folder’s contents.

Below is an example of adding the older collection to the dump folder.

Mongodump --user=user123 --db=database1 --password=mydatabse123 --authenticationDatabase=admin --out=action_figures --collection=older

The database/database1 folder would be created with the older.metadata.json and older.bson files appended, resulting in a structure that looks like this:

.
 |_action_figures
   |_database1
     |_action_figures.metadata.json
     |_action_figures.bson
     |_older.metadata.json
     |_older.bson

Use the mongodump Command to Create a MongoDB Dump of All Databases

It’s also feasible to backup and archive all the files.

Emptying everything into a dump directory is not a good idea. This option is most helpful in moving data between hosts or transmitting backup files between servers.

It uses the -archive switch to allow the user to name the archive file. This option generates a single file that we may use with mongorestore to reimport the database.

Because of this, the user cannot use both the -archive and -out options simultaneously.

The mongodump command will dump all databases (collections) in the following example:

mongodump --db=database1 --username=uberuser --password=mydatabse123 --authenticationDatabase=admin --archive=database1.archive

Use the mongorestore Command to Restore a Mongo Database

The mongorestore program is the polar opposite of mongodump, allowing users to restore the database. The application reads data from a binary database dump or the Mongodump tool.

mongorestore is different from mongoimport because it just inserts data.

The application is unable to replace existing documents in the database. It contains any necessary upgrades.

If the document’s id already exists, the document will not be replaced. Otherwise, mongorestore can establish a new database or update one that already exists.

The only condition for running mongorestore is to have the path to the dump directory. The following mongorestore example can be used:

mongorestore dump/

If localhost is specified as the host, and the database names match the names of the sub-folders in the dump directory, the databases will be generated. When utilizing a remote host, the command is more complicated.

The user must add the -uri flag or all of the regular connection flags, such as:

--host
--db
--username
--port
--password

The application also does not necessitate the restoration of the entire database. Only a single collection or a list of collections can be restored.

The user can use the -collection and -db options and enter the location of the BSON file. In this scenario, -collection refers to the database collection’s name:

mongorestore --db=newdb --collection=novels dump/mydb/product.bson

While this command is functional, it isn’t optimal. The option -nsInclude is the preferred way for restoring various collections.

This option allows the user to select a namespace pattern for restoring mongo database collections.

For example, the final structure of the folder might look like this if the dump directory dropped databases named database and database2:

.
 |_dump
   |_database
     |_product.metadata.json
     |_product.bson
     |_order.metadata.json
     |_order.bson
   |_db2
     |_product.metadata.json
     |_product.bson
     |_order.metadata.json
     |_order.bson

database might be separated and imported to restore in the local environment with -nsInclude. The command would be as follows:

mongorestore --db=database1 --nsInclude="database.*" dump/

All of the collections in database spilled from the database database1 would be restored using the command above. However, even though the data is saved in the same dump directory, the operation did not fix anything from database2.

Conclusion

As discussed in this post, mongodump is a helpful tool that allows you to back up collections with a few instructions. The entire collection may be spat out into a single file with only one command.

The program is flexible enough to back up only the required bits of the database, and it has several choices for changing the data you need to save.

There have been many technologies developed to make the task of managing databases simpler. Complex processes that must be repeated are completed fast and cleanly with these vital tools and instructions.

The entire database, or specific sections, maybe backed up or restored with a single command. mongodump can be used while dealing with MongoDB databases (collections).

Related Article - MongoDB Database