Technology Encyclopedia Home >How to set parameters when exporting data from a MongoDB database?

How to set parameters when exporting data from a MongoDB database?

When exporting data from a MongoDB database, you typically use the mongodump utility. Here’s how to set parameters for the export process:

  1. Basic Command Structure:
    The basic syntax is mongodump --uri="mongodb://[username:password@]host[:port]/database" --collection=collectionName --out=outputDirectory.

  2. Common Parameters:

    • --uri: Specifies the connection string, including username, password, host, port, and database name.
      Example: --uri="mongodb://admin:password@localhost:27017/mydb".
    • --collection: Limits the export to a specific collection.
      Example: --collection=users.
    • --out: Defines the output directory for the dumped files.
      Example: --out=/backup/mongodb.
    • --gzip: Compresses the output files.
      Example: --gzip.
  3. Authentication Parameters:
    If authentication is required, include --username and --password (or use --uri for simplicity).
    Example: --username=admin --password=password.

  4. Filtering Data:
    Use --query to export specific documents.
    Example: --query='{ "age": { "$gt": 30 } }'.

  5. TLS/SSL Options:
    For secure connections, use --ssl and provide certificate paths if needed.
    Example: --ssl --sslCAFile=/path/to/ca.pem.

  6. Example Command:

    mongodump --uri="mongodb://admin:password@localhost:27017/mydb" --collection=users --out=/backup/mongodb --gzip
    

For managed MongoDB services like Tencent Cloud’s TencentDB for MongoDB, you can use the Data Transmission Service (DTS) or Backup & Restore features to export data with similar parameter configurations via the console or API. Tencent Cloud also supports automated backups and cross-region replication for data export scenarios.