Study guide

8.2 Data Ingestion & Export Utilities

MongoDB Associate Developer Exam

Track your progress as you read

Finished sections get a check mark.

1 Chapter 1: Document Model & MongoDB Architecture

1.1 Document Model Fundamentals & BSON Data Types 1.2 Databases, Collections & Schema Flexibility 1.3 Distributed Architecture Fundamentals: Replica Sets & Sharded Clusters

2 Chapter 2: CRUD Operations: Create & Read

2.1 Document Insertion Operations 2.2 Query Selectors & Filter Operators 2.3 Array Queries & Element Evaluation 2.4 Projections, Cursor Methods & Collation

3 Chapter 3: CRUD Operations: Update & Delete

3.1 Field Update Operators 3.2 Array Update Operators & Positional Modifiers 3.3 Document Replacement & Deletion Operations 3.4 Atomic Find-and-Modify Operations

4 Chapter 4: Aggregation Framework Mastery

4.1 Pipeline Fundamentals & Core Stages ($match, $project, $addFields) 4.2 Grouping, Reshaping & Array Manipulation ($group, $unwind, $replaceWith) 4.3 Multi-Document Joins & Faceted Pipelines ($lookup, $facet, $bucket) 4.4 Materialization & Window Operations ($out, $merge, $setWindowFields) 4.5 Atlas Search: Search Index Definitions & $search Queries

5 Chapter 5: Indexing Strategies & Performance Optimization

5.1 Index Types & Storage Mechanics 5.2 Compound Indexes & the Equality-Sort-Range (ESR) Rule 5.3 Multikey Indexes & Array Indexing Constraints 5.4 Specialized Index Types: TTL, Partial, Unique & Text 5.5 Query Optimization & Explain Plan Analysis

6 Chapter 6: Data Modeling & Schema Design Patterns

6.1 Embedding vs. Referencing Tradeoffs 6.2 Core Schema Design Patterns 6.3 Schema Validation with JSON Schema

7 Chapter 7: Application Development & Driver Integration

7.1 Connection Strings & Connection Pool Management 7.2 Driver CRUD Paradigms & Object Mapping 7.3 Cursor Batching, Streaming & Memory Handling 7.4 Write Concerns, Read Concerns & Retryable Operations

8 Chapter 8: Developer Tooling & Administration Essentials

8.1 mongosh Scripting & Administrative Commands 8.2 Data Ingestion & Export Utilities 8.3 Backup, Restore & Performance Telemetry 8.4 Atlas Sample Dataset & Data Explorer

8.2 Data Ingestion & Export Utilities

Key Takeaways Explain This Topic AI

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 Match Behavior on No Match Duplicate Key Conflict Handling
  • insert (Default) Attempts insert Inserts new document Fails or logs error; does not modify existing document

upsert Replaces the existing document entirely with the new document Inserts new document Matches document via --upsertFields (defaults to _id ) and overwrites it

merge Updates / merges fields from input data into the existing document Inserts new document Matches 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 } :

--mode=insert : The import operation detects a duplicate key error on _id: 101 and skips/logs an error. The existing document remains completely unchanged.

--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 !

--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 Type Canonical 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 9007199254740993 Number 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:

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^{53} - 1 2 53 − 1 .

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 ).

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 Dimension CLI Tools ( mongoimport / mongoexport ) Programmatic Driver bulkWrite() Pipeline

Primary Use Case Ad-hoc data loading, spreadsheet export, quick CSV/JSON feeds Production 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 Handling Basic skip/halt on error; logs to stderr Fine-grained programmatic error handling ( BulkWriteException )

Execution Speed Fast C++/Go optimized multi-threaded ingestion Maximum throughput via customizable batching and connection pools

Operational Overhead Zero 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