Technology Encyclopedia Home >How to export MongoDB data to local using mongodump (entire database) or mongoexport (single collection)?

How to export MongoDB data to local using mongodump (entire database) or mongoexport (single collection)?

To export MongoDB data to your local machine, you can use either mongodump for the entire database or mongoexport for a single collection. Here's how to do both with examples:

1. Using mongodump (Entire Database)

mongodump exports the entire database or specific collections into a binary format (BSON).

Command Syntax:

mongodump --uri="mongodb://<username>:<password>@<host>:<port>/<database>" --out=<local_directory>

Example:
Export the entire mydb database to the ./backup folder:

mongodump --uri="mongodb://admin:password123@localhost:27017/mydb" --out=./backup

Notes:

  • If no authentication is required, omit --uri and use --db=<database_name>.
  • The output will be a folder structure with .bson files for each collection.

2. Using mongoexport (Single Collection)

mongoexport exports data from a single collection into a JSON or CSV file.

Command Syntax:

mongoexport --uri="mongodb://<username>:<password>@<host>:<port>/<database>" --collection=<collection_name> --out=<local_file_path>

Example (JSON Output):
Export the users collection from mydb to users.json:

mongoexport --uri="mongodb://admin:password123@localhost:27017/mydb" --collection=users --out=./users.json

Example (CSV Output):
Export specific fields (name, email) to users.csv:

mongoexport --uri="mongodb://admin:password123@localhost:27017/mydb" --collection=users --type=csv --fields=name,email --out=./users.csv

Notes:

  • Use --type=csv for CSV output and specify fields with --fields.
  • If no authentication is needed, omit --uri and use --db=<database_name> --collection=<collection_name>.

Tencent Cloud Recommendation

For managed MongoDB services on Tencent Cloud, use TencentDB for MongoDB, which provides automated backups. You can also use the MongoDB Shell or MongoBooster (a GUI tool) to run these commands securely.

For large-scale exports, consider Tencent Cloud Object Storage (COS) to store backups and integrate with Data Transmission Service (DTS) for cross-region replication.