0PricingLogin
MongoDB Academy · Lesson

mongoexport: Dumping Collections to Files

Learners will export a collection to JSON or CSV format using mongoexport, selecting specific fields and applying filters.

What Is mongoexport?

mongoexport is a MongoDB Database Tools command-line utility that reads documents from a collection and writes them to a file. It outputs data in JSON (JSONL format, one document per line) or CSV format. Common uses include creating data backups for analysis, migrating data to another system, sharing snapshots for debugging, or seeding other environments from a production sample.

Basic JSON Export

The simplest export command specifies the source database, collection, and output file. By default mongoexport writes JSONL—one JSON document per line—which is ideal for streaming large exports and re-importing with mongoimport. Use --out to name the output file; without it, documents print to stdout.

# Export the 'products' collection to JSONL
mongoimport \
  --uri 'mongodb://localhost:27017/myapp' \
  --collection products \
  --out products_export.json

# Each line in the output file is one document:
# {"_id":{"$oid":"..."},"name":"Widget","price":9.99}
# {"_id":{"$oid":"..."},"name":"Gadget","price":19.99}

All lessons in this course

  1. mongoimport: Loading JSON and CSV Files
  2. mongoexport: Dumping Collections to Files
  3. mongodump and mongorestore for Full Backups
  4. Seeding Data With Node.js Scripts
← Back to MongoDB Academy