JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

Create a MongoDB dump of the database

Author:JIYIK Last Updated:2025/04/29 Views:

In this MongoDB article, you’ll get a walkthrough of Mongodumpand Mongorestore, how to use them, and some simple examples of backing up and restoring your collections using both tools.

mongodumpCommands in MongoDB

Mongodump is a tool that creates a binary export from the contents of a database. This tool supports mongodand mongosinstances.

Users can use Mongodump to export data from standalone, replica, collection, and sharded cluster installations.

Mongodump used to be updated regularly, with a new version available whenever the MongoDB server was upgraded. However, the utility has been versioned since MongoDB 4.4. The current version 100.2.1 supports MongoDB 3.6, 4.0, 4.2, and 4.4.

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

If IT professionals want to schedule backups (collections), this is one way they can back up and restore databases on a daily basis. For example, Mongodump can save everything in one file, and Mongorestore can be used later to fully restore the database.

We can execute mongodumpcommands from the system command line instead of the mongo shell. mongodumpThe command structure is as follows:

mongodump <options> <connection-string>

-uriand properly structured string or flag options like -user, -dband -passwordcan be used to connect to a mongo database. Unfortunately, users cannot combine two different commands into one.

mongodumpCreate a MongoDB dump of the database using the command

mongodumpYou can use localhost to dump database1a collection named using the following command, using the URI format and user information.

mongodump --uri="mongodb://uberuser:mydatabse123@localhost:27107/database1?ssl=false&authSource=admin"

mongodumpAnother example of the command using the standard flags is as follows:

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

Database backups can also be saved as archive files. Dumping files into a directory is not good in comparison.

These selections are used to switch servers or transfer data across hosts. -archiveThe option allows the user to provide a name for the archive.

This option generates a file that can be used to Mongorestore.re-import the database.

If the name of the database is the same as the database you want to dump, use -authenticationDatabasethe flag with the correct name. When using a URI, make sure authSourcethe component links to the correct database.

The typical mongodumpapproach dumps the entire database into a single dump directory, named by default dump.. This directory will be created in the same directory from which we run the command.

The database will be named after the subfolder in the directory. In the previous example, this would be database1; therefore, the new structure would be./dump/database1.

The database collection in the corresponding folder will have two separate files - a BSON and a JSON file.

.metadate.jsonThe files will follow a similar structure, containing metadata such as options, indexes,and nsto match the namespace of the collection. The files in the BSON file .bsonwill hold the data for the collection.

Users can mongodump.customize specific behaviors of the output in .

You can use -outflags such as -dump-directory to provide the name of the directory where the database should be dumped. For example, the dump directory could be called -dump dumbbase.instead of -dump.

Here is how the command will appear.

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

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

The user can further limit the functionality of the utility by backing up only one collection at a time. The user can -collectionspecify the collection to be dumped using the -dump parameter.

If you are dumping only 行动人物the collection, an example mongodumpcommand would be:

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

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

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

This command can be used as many times as the user needs to back up one collection at a time. These instructions will not overwrite the contents of the output folder.

The following is olderan example of adding a collection to a dump folder.

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

database/database1A folder will be created with the appended older.metadata.jsonand older.bsonfiles and its structure will look like this:

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

mongodumpCreate a MongoDB dump of all databases using the command

It is also possible to backup and archive all files.

It is not a good idea to flush everything to the dump directory. This option is most useful for moving data between hosts or transferring backup files between servers.

It uses -archivethe switch to allow the user to name the archive file. This option generates a file that we can mongorestorere-import the database using .

Therefore, users cannot use the -archiveand -outoptions at the same time.

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

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

Use mongorestorethe command to restore the Mongo database

mongorestoreThe program does mongodumpthe exact opposite of , allowing the user to restore a database. The application reads data from a binary database dump or the Mongodump tool.

mongorestoreDifferent from mongoimportbecause it just inserts the data.

The application cannot replace existing documents in the database. It contains any necessary upgrades.

If a document with the same id already exists, the document will not be replaced. Otherwise, mongorestorea new database will be created or an existing one will be updated.

The only requirement for running mongorestoreis to have the path to the dump directory. The following mongorestoreexample can be used:

mongorestore dump/

If localhost is specified as the host, and the database name matches the name of the subfolder in the dump directory, the database will be generated. When a remote host is used, the commands are more complicated.

The user must add -urithe flag or all the regular connection flags, for example:

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

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

The user can use the -collectionand -dboptions and enter the location of the BSON file. In this case, -collectionit is the name of the database collection:

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

While this command works, it is not optimal. -nsIncludeThe -r option is the preferred way to restore various collections.

This option allows the user to select the namespace mode used to restore the mongo database collections.

For example, if the dump directory deletes databases named databaseand database2, the final structure of the folders might look like this:

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

databaseMay be detached and imported for use in a local environment -nsInclude.. The command is as follows:

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

All collections in the database that database1were spilled databasewill be restored using the above command. However, even though the data was saved in the same dump directory, this operation does not repair database2anything in .

in conclusion

As discussed in this article, mongodumpis a useful tool that allows you to back up a collection with a few instructions. The entire collection may be spit out into a file with just a single command.

The program is flexible enough to back up only the desired bits of your database, and it has multiple options for changing the data you need to save.

Many technologies have been developed to simplify the task of managing databases. Complex processes that must be repeated can be completed quickly and cleanly using these important tools and instructions.

The entire database or a specific portion can be backed up or restored with a single command. mongodumpCan be used when working with MongoDB databases (collections).

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

List all collections in MongoDB Shell

Publish Date:2025/04/29 Views:156 Category:MongoDB

When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c

Querying for non-null values in MongoDB

Publish Date:2025/04/29 Views:139 Category:MongoDB

This MongoDB article will explain how to query for non-null values ​​in MongoDB. To query for non-null values, you can use $ne the operator and $eq the operator and then specify the desired value to query. This article shows the readers

Shutting down with code:100 error in MongoDB

Publish Date:2025/04/29 Views:53 Category:MongoDB

This MongoDB tutorial will teach you to fix the error on different operating systems shutting down with code:100 . It will also talk about the root cause of why this issue occurs. shutting down with code:100 Errors in MongoDB As we all know

SELECT COUNT GROUP BY in MongoDB

Publish Date:2025/04/29 Views:74 Category:MongoDB

In this article, we will discuss the functions in MongoDB. Also, we will point out the aggregation functions in detail. We will explain in detail the different ways to count and sort multiple and single fields of Group in MongoDB. Operation

Differences between MongoDB and Mongoose

Publish Date:2025/04/29 Views:80 Category:MongoDB

This MongoDB article will discuss the differences between MongoDB and Mongoose. Unfortunately, most beginners tend to confuse these two concepts when they start developing applications and use MongoDB as their backend. MongoDB has its own s

Install MongoDB using Homebrew

Publish Date:2025/04/29 Views:161 Category:MongoDB

MongoDB is a well-known unstructured database management system that can handle large amounts of data. It is a document-oriented database system and belongs to the NoSQL family (non-SQL). Data and records are stored as documents that look a

Use find operator to join multiple conditions in MongoDB

Publish Date:2025/04/29 Views:132 Category:MongoDB

$lookup Today, we will see how to concatenate multiple conditions using the operator in MongoDB . In addition, we will explore some examples to demonstrate the use of $group the stage and $unionWidth the aggregation stage. $lookup Use the o

Date comparison in MongoDB

Publish Date:2025/04/28 Views:170 Category:MongoDB

This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie

Find objects between two dates in MongoDB

Publish Date:2025/04/28 Views:95 Category:MongoDB

In this article, the problem of finding objects between two dates was briefly discussed. In addition, the operators $gte, $lte, $gt, and $lt used for this purpose were briefly explained in detail. Querying date ranges in MongoDB This sectio

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial