Back to Blog

MongoDB JSON Export to Excel: Complete Guide (2026)

MongoDB stores data in a flexible, document-based format (BSON). This is great for developers, but terrible when your CFO asks for an Excel report.

This guide shows you how to export MongoDB data to Excel without losing nested documents or breaking array fields.

The Problem with Standard MongoDB Exports

Default mongoexport Output

Running this command:

mongoexport --db=mydb --collection=users --out=users.json

Gives you JSON like this:

{
  "_id": {"$oid": "507f1f77bcf86cd799439011"},
  "name": "John Doe",
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "zip": "10001"
  },
  "orders": [
    {"item": "Laptop", "price": 1200},
    {"item": "Mouse", "price": 25}
  ],
  "createdAt": {"$date": "2024-01-15T10:30:00.000Z"}
}

Problems When Opening in Excel

  1. Nested Objects Show as [Object]
    The address field becomes unreadable

  2. Arrays Become [object Object],[object Object]
    You can't see individual orders

  3. Special BSON Types Fail

    • _id shows as {"$oid": "..."} instead of the actual ID
    • Dates show as {"$date": "..."} instead of Excel date format
  4. No Column Headers
    Nested fields don't get proper column names

Solution 1: Use MongoDB Compass (Limited)

MongoDB Compass has a built-in CSV export:

  1. Connect to your database
  2. Select collection
  3. Click "Export" → "Export Collection"
  4. Choose "CSV"

Limitations:

  • ❌ Can only export visible columns (max 20)
  • ❌ Nested documents still show as [object Object]
  • ❌ No array flattening
  • ❌ File size limit of ~100MB

Good for quick exports, terrible for complex data.

Solution 2: Flatten with JsonExport

Step 1: Export from MongoDB

Use mongoexport with pretty formatting:

mongoexport --db=mydb --collection=users --jsonArray --pretty --out=export.json

The --jsonArray flag wraps all documents in an array, which is easier to work with.

Step 2: Convert to Excel

  1. Open JsonExport
  2. Upload your export.json file (or paste the JSON)
  3. Switch to "Table View"
    This is smart mode that handles nested data
  4. Click "Download Excel"

What Happens Automatically:

Nested objects become columns:

address.street | address.city | address.zip
123 Main St    | New York     | 10001

Arrays get flattened with indices:

orders.0.item | orders.0.price | orders.1.item | orders.1.price
Laptop        | 1200           | Mouse         | 25

BSON types get cleaned:

  • _id507f1f77bcf86cd799439011 (just the ID)
  • createdAt2024-01-15 10:30:00 (readable timestamp)

Handling Different MongoDB Schemas

1. Variable Array Lengths

Problem: Some users have 2 orders, some have 10.

Standard CSV export:

  • Creates columns for ALL possible array indices
  • Results in 100+ empty columns

JsonExport approach:

  • Uses "Nested View" mode
  • Creates a parent sheet (Users) and child sheet (Orders)
  • You can join them in Excel with VLOOKUP or Power Query

2. Deep Nesting

Example structure:

{
  "company": {
    "departments": {
      "engineering": {
        "teams": ["frontend", "backend"]
      }
    }
  }
}

Becomes:

company.departments.engineering.teams.0 | company.departments.engineering.teams.1
frontend                                  | backend

Dot notation preserves the full path.

3. Mixed Data Types

MongoDB allows inconsistent schemas:

// Document 1
{"tags": ["red", "blue"]}

// Document 2
{"tags": "green"}

JsonExport detects this and:

  • Converts single values to arrays for consistency
  • OR creates separate columns (tags vs tags.0)

Advanced: Using Aggregation Pipeline

For complex exports, use MongoDB aggregation before exporting.

Example: Unwind Arrays Before Export

db.users.aggregate([
  {
    $unwind: "$orders"
  },
  {
    $project: {
      name: 1,
      "address.city": 1,
      orderItem: "$orders.item",
      orderPrice: "$orders.price"
    }
  },
  {
    $out: "users_flattened"
  }
])

Then export the flattened collection:

mongoexport --db=mydb --collection=users_flattened --type=csv --fields=name,address.city,orderItem,orderPrice --out=users.csv

This gives you a perfectly flat CSV, but requires MongoDB query knowledge.

Comparison: Methods

MethodNested DataArraysFile SizeEase
Compass CSV❌ Breaks❌ Breaks100MB maxEasy
mongoexport CSV❌ No nesting❌ No arraysUnlimitedMedium
Aggregation + export✅ Manual✅ ManualUnlimitedHard
JsonExport✅ Auto✅ Auto50MB+ (in-browser)Easy

Real-World Use Case: E-Commerce Analytics

You have a products collection:

{
  "name": "T-Shirt",
  "variants": [
    {"size": "S", "color": "red", "stock": 10},
    {"size": "M", "color": "blue", "stock": 5}
  ],
  "reviews": [
    {"user": "Alice", "rating": 5},
    {"user": "Bob", "rating": 4}
  ]
}

Your marketing team needs an Excel file with:

  • One row per product variant
  • Average rating calculated

Manual Approach (30+ minutes):

  1. Write aggregation pipeline
  2. Export to JSON
  3. Write Python script to flatten
  4. Calculate rating average
  5. Import to Excel

JsonExport Approach (2 minutes):

  1. Export collection to JSON
  2. Upload to JsonExport
  3. Switch to "Nested View"
  4. Download Excel with variant + review sheets
  5. Use Excel formulas to calculate averages

Handling Large Exports

MongoDB collections with 100K+ documents:

Option 1: Batch Processing

# Export in chunks
mongoexport --db=mydb --collection=users --skip=0 --limit=10000 > batch1.json
mongoexport --db=mydb --collection=users --skip=10000 --limit=10000 > batch2.json

Then convert each batch separately.

Option 2: Filter Before Export

mongoexport --db=mydb --collection=orders --query='{"createdAt": {"$gte": {"$date": "2024-01-01"}}}' --out=recent_orders.json

Only export what you need.

Privacy Considerations

MongoDB exports often contain:

  • Customer emails
  • Financial data
  • Sensitive business metrics

JsonExport processes everything client-side:

  • ✅ No file upload to servers
  • ✅ Processing happens in your browser
  • ✅ Data never leaves your machine

Common Errors and Fixes

Error: "E11000 duplicate key error"

This is a MongoDB error, not related to export. Your collection has duplicate _id values (rare).

Fix: Export without _id field:

mongoexport --db=mydb --collection=users --fields=name,email,address --out=users.json

Error: "Expected ',' instead of '}'"

Your JSON export is malformed. Usually happens with:

  • Connection interruptions
  • Disk space issues during export

Fix: Re-run the export with --jsonArray flag.

Error: File too large

JsonExport handles files up to 100MB. For even larger exports:

Solutions:

  1. Filter your export (--query)
  2. Split into batches (--skip, --limit)
  3. Export only needed fields (--fields)
  4. Use Python with Pandas for large datasets

Complete Workflow Example

Scenario: Export User Orders for Q1 2024

  1. MongoDB Query:
mongoexport \
  --db=ecommerce \
  --collection=users \
  --query='{"createdAt": {"$gte": {"$date": "2024-01-01"}, "$lte": {"$date": "2024-03-31"}}}' \
  --jsonArray \
  --out=q1_users.json
  1. Convert:
  • Open JsonExport
  • Upload q1_users.json
  • Review preview in "Table View"
  • Download as q1_users.xlsx
  1. Analyze in Excel:
  • Use Power Query to join user and order sheets
  • Create PivotTables for revenue by month
  • Share with stakeholders

Time saved: ~2 hours vs writing custom scripts.

Conclusion

MongoDB's flexible schema is powerful for development, but creates headaches for business reporting.

Instead of:

  • Writing complex aggregation pipelines
  • Learning Python/JavaScript for data transformation
  • Using multiple tools (export, transform, import)

Use JsonExport to go from MongoDB → Excel in 2 minutes.

Convert Your MongoDB Export Now


Related Articles: