0Pricing
MongoDB Academy · บทเรียน

mongoexport: การส่งออกคอลเลกชันเป็นไฟล์

ผู้เรียนจะส่งออกคอลเลกชันเป็นรูปแบบ JSON หรือ CSV ด้วย mongoexport โดยเลือกฟิลด์ที่ต้องการและใช้ตัวกรอง

mongoexport: การส่งออกคอลเลกชันเป็นไฟล์ เป็นบทเรียน MongoDB Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MongoDB Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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}

Exporting to CSV Format

Use --type csv to export as comma-separated values. CSV exports require you to list the fields to include with --fields—mongoexport cannot infer all fields from a schema-flexible collection automatically. The first row of the CSV output contains the column headers matching the fields you specified.

# Export selected fields as CSV
mongoimport \
  --uri 'mongodb://localhost:27017/myapp' \
  --collection products \
  --type csv \
  --fields 'name,price,category,rating' \
  --out products_export.csv

# Output:
# name,price,category,rating
# Widget,9.99,tools,4.5
# Gadget,19.99,electronics,4.2

Filtering Documents to Export

Use the --query flag with a JSON filter document to export only the documents that match a condition, instead of the entire collection. This is useful for extracting a subset—such as all orders from a specific month, or all users with a certain role—without exporting data you don't need.

# Export only shipped orders from January 2024
mongoimport \
  --uri 'mongodb://localhost:27017/myapp' \
  --collection orders \
  --query '{"status":"shipped","createdAt":{"$gte":{"$date":"2024-01-01T00:00:00Z"},"$lt":{"$date":"2024-02-01T00:00:00Z"}}}' \
  --out january_orders.json

Selecting Specific Fields to Export

Use --fields (or -f) with a comma-separated list of field names to include in the output. Fields not listed are excluded. This is equivalent to an inclusion projection in a find query. Combined with --query, you can export precisely the data slice you need, keeping file sizes small and sharing only relevant fields.

# Export only name, email, and createdAt for active users
mongoimport \
  --uri 'mongodb://localhost:27017/myapp' \
  --collection users \
  --query '{"isActive":true}' \
  --fields 'name,email,createdAt' \
  --out active_users.json

# Sensitive fields like passwordHash are automatically excluded

Sorting the Export Output

Use --sort with a JSON sort document to export documents in a specific order. This is useful when you need the output file to be ordered for downstream processing—for example, exporting events in chronological order for replay, or exporting users alphabetically for a report.

# Export events in chronological order
mongoimport \
  --uri 'mongodb://localhost:27017/myapp' \
  --collection events \
  --sort '{"timestamp":1}' \
  --out events_sorted.json

Limiting the Export Count

Use --limit to export at most N documents and --skip to skip the first N documents. These flags let you export a sample—for example, the 1,000 most recent orders—or export in batches by paginating through the collection with repeated calls using skip offsets. Use with a --sort to make the limit meaningful.

# Export the 100 most recently created products (sample for testing)
mongoimport \
  --uri 'mongodb://localhost:27017/myapp' \
  --collection products \
  --sort '{"createdAt":-1}' \
  --limit 100 \
  --out products_sample.json

Extended JSON vs Relaxed Mode

mongoexport outputs Extended JSON by default, which uses special wrapper objects to represent BSON types not native to JSON—for example, {"$oid":"..."} for ObjectId and {"$date":"..."} for dates. Use --relaxed to output relaxed Extended JSON where dates become ISO strings and numbers are plain JSON numbers—more readable but loses some type fidelity for re-import.

# Default (Canonical Extended JSON)
# {"_id":{"$oid":"65a..."},"price":{"$numberDecimal":"9.99"}}

# Relaxed Extended JSON — more human-readable
mongoimport \
  --uri 'mongodb://localhost:27017/myapp' \
  --collection products \
  --relaxed \
  --out products_relaxed.json
# {"_id":"65a...","price":9.99}

Exporting From MongoDB Atlas

Use the Atlas cluster's SRV connection string in the --uri flag to export from an Atlas cluster. Atlas requires TLS (automatically enabled with SRV URIs). For very large Atlas collections, prefer the Atlas UI's 'Export Collection' button, or use Atlas Data Federation for analytical exports to S3 or other data lakes.

# Export from Atlas cluster
MONGO_URI='mongodb+srv://user:password@cluster0.abc.mongodb.net/myapp'

mongoimport \
  --uri "$MONGO_URI" \
  --collection orders \
  --query '{"status":"completed"}' \
  --out completed_orders.json

mongoexport vs mongodump

Choose the right tool for your use case:

  • mongoexport: outputs human-readable JSON or CSV; suitable for data sharing, analysis, or selective migration; can filter and project
  • mongodump: outputs binary BSON; full collection or database backups; faster and preserves all BSON types losslessly; not human-readable
For backups, prefer mongodump. For data sharing or selective exports, use mongoexport.

Piping Export Output to Another Command

Without the --out flag, mongoexport prints to stdout, enabling Unix pipe compositions. You can pipe the output to wc -l to count documents, to grep to filter lines, to jq for JSON processing, or to another command entirely. This makes mongoexport a flexible component in data pipeline scripts.

# Count documents exported
mongoimport --uri '...' --collection products | wc -l

# Pipe to jq to extract just product names
mongoimport --uri '...' --collection products | jq -r '.name'

# Compress while exporting
mongoimport --uri '...' --collection products | gzip > products.json.gz

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: mongoexport outputs JSONL or CSV from a collection with optional filter, field selection, and sort, --query lets you export a targeted subset of documents, and mongodump is preferred for full backups while mongoexport is better for selective, human-readable exports. Next up we explore mongodump and mongorestore for full binary database backups.

คำถามที่พบบ่อย

บทเรียน “mongoexport: การส่งออกคอลเลกชันเป็นไฟล์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “mongoexport: การส่งออกคอลเลกชันเป็นไฟล์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MongoDB Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “mongoexport: การส่งออกคอลเลกชันเป็นไฟล์”

ผู้เรียนจะส่งออกคอลเลกชันเป็นรูปแบบ JSON หรือ CSV ด้วย mongoexport โดยเลือกฟิลด์ที่ต้องการและใช้ตัวกรอง คุณปฏิบัติ MongoDB Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MongoDB Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MongoDB Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “mongoexport: การส่งออกคอลเลกชันเป็นไฟล์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MongoDB Academy นี้ได้ไหม

ได้ บทเรียน MongoDB Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. mongoimport: การโหลดไฟล์ JSON และ CSV
  2. mongoexport: การส่งออกคอลเลกชันเป็นไฟล์
  3. mongodump และ mongorestore สำหรับการสำรองข้อมูลทั้งหมด
  4. การเตรียมข้อมูลเริ่มต้นด้วยสคริปต์ Node.js
← กลับไปที่ MongoDB Academy