8.2 Data Ingestion & Export Utilities

Key Takeaways

  • mongoimport and mongoexport are standalone CLI tools within the Database Tools suite used for importing and exporting data in JSON, CSV, and TSV formats.
  • mongoimport supports three ingestion modes: 'insert' (default), 'upsert' (replaces matching documents on --upsertFields), and 'merge' (updates specified fields while preserving existing untouched fields).
  • Key mongoimport flags include --drop (clears target collection), --headerline (parses CSV headers), --ignoreBlanks (omits empty fields), and --columnsHaveTypes for explicit type casting.
  • mongoexport outputs collection data in Extended JSON (Canonical or Relaxed) or CSV, supporting query filtering with --query and projection with --fields.
  • Exporting to plain JSON or CSV causes BSON type fidelity loss, making programmatic driver bulkWrite() pipelines mandatory for production ETL requiring exact typing and ACID transactions.
Last updated: September 2026

8.2 Data Ingestion & Export Utilities

In enterprise application development and operations, developers frequently need to load initial seed data, ingest external datasets from third-party partners (such as CSV or JSON feeds), and export database collections for reporting, analytics, or external system integration. MongoDB provides two specialized command-line utilities within the MongoDB Database Tools suite for this purpose: mongoimport and mongoexport.

Understanding how to configure these tools, select appropriate ingestion modes (insert, upsert, merge), manage schema headers, and evaluate the trade-offs of BSON type fidelity loss versus programmatic driver bulk writes is essential for the MongoDB Certified Associate Developer Exam.


1. Overview of MongoDB Database Tools Architecture

Unlike mongosh, mongoimport and mongoexport are standalone command-line binaries executed from the operating system shell (e.g., Bash, Zsh, or PowerShell), not from within mongosh. They establish their own direct TCP connections to the MongoDB cluster via standard connection strings (--uri).

Common Connection Options for CLI Tools

All MongoDB Database Tools share a unified set of connection and authentication parameters:

# Standard connection using a URI string
mongoimport --uri="mongodb+srv://dbUser:SecretPass@cluster0.example.com/ecommerce" ...

# Explicit host, port, and authentication flags
mongoexport --host=localhost --port=27017 --db=inventory --collection=products \
  --username=appAdmin --password=SecretPass --authenticationDatabase=admin ...

2. mongoimport: High-Throughput Data Ingestion

mongoimport reads content from an input file or standard input (stdin) and inserts, upserts, or merges the data into a target MongoDB collection. It natively supports JSON, CSV (Comma-Separated Values), and TSV (Tab-Separated Values).

+-----------------------------------------------------------------------------------------+
|                                 mongoimport Ingestion Pipeline                          |
|                                                                                         |
|  [ File / stdin ] ---> [ Format Parser ] ---> [ Mode: insert|upsert|merge ] ---> [ DB ] |
|  (JSON/CSV/TSV)        (--columnsHaveTypes)   (Resolves on --upsertFields)              |
+-----------------------------------------------------------------------------------------+

Core Ingestion Modes (--mode)

A critical exam concept is understanding how mongoimport handles existing documents and primary key conflicts via the --mode parameter:

Mode (--mode)Behavior on MatchBehavior on No MatchDuplicate Key Conflict Handling
insert (Default)Attempts insertInserts new documentFails or logs error; does not modify existing document
upsertReplaces the existing document entirely with the new documentInserts new documentMatches document via --upsertFields (defaults to _id) and overwrites it
mergeUpdates / merges fields from input data into the existing documentInserts new documentMatches via --upsertFields; preserves untouched fields in target document

Practical Mode Comparison Example:

Suppose a collection contains an existing document:

{ "_id": 101, "sku": "LAPTOP-X", "price": 1200, "warranty": "2yr", "warehouse": "Austin" }

Now, mongoimport processes an incoming record with { "_id": 101, "sku": "LAPTOP-X", "price": 1150 }:

  1. --mode=insert: The import operation detects a duplicate key error on _id: 101 and skips/logs an error. The existing document remains completely unchanged.
  2. --mode=upsert --upsertFields=_id: The existing document is completely replaced. The resulting document becomes { "_id": 101, "sku": "LAPTOP-X", "price": 1150 }. The unmentioned fields (warranty, warehouse) are deleted/lost!
  3. --mode=merge --upsertFields=_id: The input fields are applied via an update. The resulting document is { "_id": 101, "sku": "LAPTOP-X", "price": 1150, "warranty": "2yr", "warehouse": "Austin" }. The existing warranty and warehouse fields are safely preserved.

Critical mongoimport Command-Line Options

  • --file <path>: Path to the source file. If omitted, mongoimport reads from stdin.
  • --type <json|csv|tsv>: Specifies input format. Default is json.
  • --drop: Drops the target collection before importing data. Invaluable for reproducible test seeding, but highly dangerous in production.
  • --headerline: Used exclusively with --type=csv or --type=tsv. Instructs mongoimport to parse the first line of the input file as field names.
  • --fields <f1,f2,...> / --fieldFile <path>: Specifies field names manually if the CSV/TSV lacks a header row.
  • --ignoreBlanks: In CSV/TSV ingestion, ignores empty fields instead of inserting empty string fields ("") into the document.
  • --jsonArray: Required when importing a single JSON file formatted as a JSON array ([ {...}, {...} ]) rather than newline-delimited JSON (NDJSON).
  • --upsertFields <f1,f2,...>: Specifies the unique matching fields when --mode=upsert or --mode=merge is selected (defaults to _id).
  • --columnsHaveTypes: Enables explicit type casting syntax in CSV/TSV headers (e.g., age.int32(), joined.date(YYYY-MM-DD)).

Practical mongoimport Code Examples

# 1. Importing a standard newline-delimited JSON file, dropping the old collection
mongoimport --uri="mongodb://localhost:27017/shop" \
  --collection=customers \
  --file=./customers.ndjson \
  --drop

# 2. Importing a JSON Array file with explicit merge mode on customerId
mongoimport --uri="mongodb://localhost:27017/shop" \
  --collection=customers \
  --file=./customer_updates.json \
  --jsonArray \
  --mode=merge \
  --upsertFields=customerId

# 3. Importing a CSV file using the first row as headers, ignoring blank values
mongoimport --uri="mongodb://localhost:27017/shop" \
  --collection=inventory \
  --type=csv \
  --headerline \
  --ignoreBlanks \
  --file=./warehouse_stock.csv

# 4. Typed CSV Ingestion with --columnsHaveTypes
# CSV Header: sku.string(),qty.int32(),price.decimal(),active.boolean(),created.date(2006-01-02)
mongoimport --uri="mongodb://localhost:27017/shop" \
  --collection=products \
  --type=csv \
  --columnsHaveTypes \
  --file=./typed_products.csv

3. mongoexport: Data Extraction & Query Filtering

mongoexport reads documents from a MongoDB collection and writes them to an output file or stdout in JSON or CSV format. It is designed for exporting datasets for human consumption or external spreadsheet/ETL tools.

Core mongoexport Options

  • --collection <name> / -c <name>: Specifies the source collection.
  • --out <path> / -o <path>: Output destination file path. If omitted, writes to stdout.
  • --type <json|csv>: Output format (default is json). When --type=csv is selected, --fields or --fieldFile is strictly required.
  • --fields <field1,field2,...> / -f: Comma-separated list of fields to include in the export.
  • --fieldFile <path>: Path to a file containing one field name per line to include in the export.
  • --query '<json>' / -q '<json>': BSON query filter enclosed in quotes to export a specific subset of documents.
  • --sort '<json>': Sort specification to dictate export order.
  • --limit <N> / --skip <N>: Restricts the total number of exported documents or skips initial records.
  • --jsonFormat <canonical|relaxed>: Selects MongoDB Extended JSON specification format (Canonical vs. Relaxed).

Practical mongoexport Code Examples

# 1. Exporting filtered documents to newline-delimited JSON
mongoexport --uri="mongodb://localhost:27017/analytics" \
  --collection=events \
  --query='{ "eventType": "PURCHASE", "amount": { "$gte": 100 } }' \
  --out=./high_value_purchases.json

# 2. Exporting specific fields to CSV format
mongoexport --uri="mongodb://localhost:27017/analytics" \
  --collection=users \
  --type=csv \
  --fields="_id,email,tier,createdAt" \
  --sort='{ "createdAt": -1 }' \
  --limit=5000 \
  --out=./recent_users.csv

4. Extended JSON Formats & BSON Type Fidelity Loss

MongoDB stores data internally in BSON (Binary JSON), a rich binary format supporting over 20 distinct data types (including 64-bit integers, 128-bit decimal floats, dates, raw binary buffers, and ObjectIds). Standard JSON, however, natively supports only basic primitives: Strings, Numbers (all represented as IEEE 754 double-precision floats), Booleans, Arrays, Objects, and Null.

To bridge this divide, MongoDB defines the Extended JSON (v2) specification, offering two formatting modes during export:

Canonical Extended JSON vs. Relaxed Extended JSON

+-----------------------------------------------------------------------------------------+
|                        Extended JSON Formatting Modes                                   |
|                                                                                         |
|  BSON Date: ISODate("2026-09-02T10:00:00Z")                                             |
|  Canonical Mode: { "$date": { "$numberLong": "1788343200000" } } (Exact Type Fidelity) |
|  Relaxed Mode  : { "$date": "2026-09-02T10:00:00.000Z" }          (Human Readable)     |
|                                                                                         |
|  BSON Decimal128: NumberDecimal("149.99")                                               |
|  Canonical Mode: { "$numberDecimal": "149.99" }                   (Preserves Precision)|
|  Relaxed Mode  : { "$numberDecimal": "149.99" }                                        |
|                                                                                         |
|  BSON Int64: NumberLong("9223372036854775807")                                         |
|  Canonical Mode: { "$numberLong": "9223372036854775807" }        (Preserves 64-bit)   |
|  Relaxed Mode  : 9223372036854775807 or lossy float depending on standard parsers       |
+-----------------------------------------------------------------------------------------+
BSON TypeCanonical Extended JSON (--jsonFormat=canonical)Relaxed Extended JSON (--jsonFormat=relaxed)CSV Export Representation
ObjectId{ "$oid": "64d2a1b9..." }{ "$oid": "64d2a1b9..." }Plain hex string "64d2a1b9..."
Date{ "$date": { "$numberLong": "1693612800000" } }{ "$date": "2026-09-02T00:00:00Z" }ISO-8601 string or epoch
Int64 (Long){ "$numberLong": "9007199254740993" }Number 9007199254740993Number or text string
Decimal128{ "$numberDecimal": "99.99" }{ "$numberDecimal": "99.99" }String / floating-point float
Binary{ "$binary": { "base64": "...", "subType": "00" } }{ "$binary": { "base64": "...", "subType": "00" } }Base64 string or truncated

The Danger of BSON Type Fidelity Loss

When data is exported with mongoexport and subsequently imported with mongoimport using plain JSON or CSV:

  1. Integer/Double Coercion: A 64-bit integer (NumberLong) exported to CSV or relaxed JSON may be imported back into MongoDB as a 64-bit IEEE double float (Double), losing numeric precision beyond $2^{53} - 1$.
  2. Date String Coercion: A BSON Date exported to CSV becomes a raw string (e.g. "2026-09-02"). If imported without explicit --columnsHaveTypes configuration, it will be stored as a BSON String rather than a BSON Date, breaking all date-range queries ($gte, $lte) and date aggregation operators ($year, $dateToString).
  3. Array and Subdocument Flattening: In CSV exports, embedded documents and arrays are converted to stringified JSON blobs within quotes, requiring post-import parsing to restore document hierarchy.

[!IMPORTANT] Exam Rule: Never use mongoexport and mongoimport for complete production database backups or disaster recovery. Because they produce text-based JSON/CSV representations, they do not preserve exact BSON byte structures, index definitions, or collation settings. Use mongodump and mongorestore for full-fidelity backups.


5. Decision Matrix: CLI Utilities vs. Driver Bulk Write Pipelines

Developers frequently must decide whether to use CLI utilities (mongoimport/mongoexport) or write custom application scripts using the programmatic Driver Bulk Write API (bulkWrite()).

Evaluation DimensionCLI Tools (mongoimport / mongoexport)Programmatic Driver bulkWrite() Pipeline
Primary Use CaseAd-hoc data loading, spreadsheet export, quick CSV/JSON feedsProduction ETL, real-time application pipelines, microservice sync
BSON Type Fidelity⚠️ Lossy in CSV/Relaxed JSON; requires manual typing✅ 100% exact BSON type preservation
Data Transformation❌ Minimal (basic field mapping/type casting only)✅ Complex in-memory transformation, enrichment, business validation
Transaction Support❌ None; each batch commits independently✅ Multi-document ACID transactions (ClientSession) supported
Error HandlingBasic skip/halt on error; logs to stderrFine-grained programmatic error handling (BulkWriteException)
Execution SpeedFast C++/Go optimized multi-threaded ingestionMaximum throughput via customizable batching and connection pools
Operational OverheadZero application code required (single CLI command)Requires writing, compiling, testing, and maintaining driver code
Loading diagram...
Ingestion & Export Data Pathways: CLI Utilities vs Programmatic Driver Bulk Pipelines
Test Your Knowledge

A developer needs to import daily product catalog updates from a CSV file. For products that already exist in the database, the import must update only the fields present in the CSV file (such as price and inventoryCount) without overwriting or deleting other existing fields (such as rating and reviewsList). Which mongoimport configuration must be used?

A
B
C
D
Test Your Knowledge

When exporting collection data containing 64-bit integers (NumberLong) and high-precision monetary amounts (Decimal128) using mongoexport, which format configuration guarantees that numeric data types are strictly represented without lossy floating-point conversion?

A
B
C
D
Test Your Knowledge

An engineering team is importing customer contact lists from a CSV file where some rows have missing phone number entries. If the team runs 'mongoimport --type=csv --headerline --file=contacts.csv', empty fields are inserted into MongoDB as empty strings (phone: ""). Which flag should be added to completely omit blank CSV fields from the resulting BSON documents?

A
B
C
D
Test Your Knowledge

Which of the following business scenarios strictly necessitates using a programmatic Driver bulkWrite() pipeline instead of the mongoimport CLI utility?

A
B
C
D