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:
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:
--uri and use --db=<database_name>..bson files for each collection.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:
--type=csv for CSV output and specify fields with --fields.--uri and use --db=<database_name> --collection=<collection_name>.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.