Mongodb Mongodump Mongorestore
# MongoDB Backup (mongodump) and Restore (mongorestore)
* * *
## MongoDB Data Backup
In MongoDB, we use the `mongodump` command to back up MongoDB data. This command can export all data to a specified directory.
The `mongodump` command can specify the data volume and the server to be transferred via parameters.
### Syntax
The script syntax for the `mongodump` command is as follows:
>mongodump -h dbhost -d dbname -o dbdirectory
* **-h:**
The address of the MongoDB server, for example: 127.0.0.1. You can also specify the port number: 127.0.0.1:27017
* **-d:**
The database instance to be backed up, for example: test
* **-o:**
The location where the backed-up data will be stored, for example: c:datadump. This directory needs to be created in advance. After the backup is complete, the system automatically creates a `test` directory under the `dump` directory, which stores the backup data for that database instance.
### Example
Start your `mongod` service locally on port 27017. Open a command prompt window, navigate to the `bin` directory of the MongoDB installation directory, and enter the `mongodump` command:
>mongodump
After executing the above command, the client will connect to the MongoDB service at IP 127.0.0.1 and port 27017, and back up all data to the `bin/dump/` directory. The command output is as follows:

The optional parameter list for the `mongodump` command is shown below:
| Syntax | Description | Example |
| --- | --- | --- |
| mongodump --host HOST_NAME --port PORT_NUMBER | This command will back up all MongoDB data | mongodump --host example.com --port 27017 |
| mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY | | mongodump --dbpath /data/db/ --out /data/backup/ |
| mongodump --collection COLLECTION --db DB_NAME | This command will back up a specific collection in a database. | mongodump --collection mycol --db test |
* * *
## MongoDB Data Restore
MongoDB uses the `mongorestore` command to restore backed-up data.
### Syntax
The script syntax for the `mongorestore` command is as follows:
>mongorestore -h -d dbname
* **--host , -h :**
The address of the MongoDB server, default is: localhost:27017
* **--db , -d :**
The database instance to be restored, for example: test. This name can also be different from the backup name, such as test2
* **--drop:**
When restoring, first delete the current data, then restore the backed-up data. This means that after restoration, any data added or modified after the backup will be deleted. Use with caution!
* **:**
The last parameter of `mongorestore`, which sets the location of the backup data, for example: c:datadumptest.
You cannot specify both `` and the `--dir` option at the same time. `--dir` can also be used to set the backup directory.
* **--dir:**
Specifies the backup directory.
You cannot specify both `` and the `--dir` option at the same time.
Next, we execute the following command:
>mongorestore
The output after executing the above command is as follows:

[](#)(#)
(#)[](#)
[Volcengine Coding Plan supports mainstream large models like Doubao, GLM, DeepSeek, Kimi, MiniMax, officially supplied, stable and reliable. Configuration Guide Β₯9.9/month Subscribe Now](https://maas.xfyun.cn/modelSquare?ch=maas_lm_l2E)
### Click me to share notes
YouTip