MongoDB is often called a "JSON database," but that is only half true. Under the hood it stores BSON, not JSON. Understanding the difference saves you from confusing bugs when exporting, importing, and querying data. This guide explains how they relate and how to move JSON in and out of MongoDB.

JSON vs BSON

You write queries and documents that look like JSON, but MongoDB stores them as BSON (Binary JSON) — a binary-encoded format that extends JSON with extra data types and faster traversal.

  • JSON — text, human-readable, six value types, universal.
  • BSON — binary, faster for the database to scan, supports extra types like ObjectId, native dates, binary data, and distinct int/long/double numbers.

When you export, BSON is converted to a JSON-like text format called Extended JSON, which preserves those extra types.

Extended JSON: the special types

Because JSON has no native date or ObjectId type, MongoDB represents them with special wrappers in Extended JSON:

{
  "_id": { "$oid": "507f1f77bcf86cd799439011" },
  "createdAt": { "$date": "2026-01-15T10:00:00Z" },
  "count": { "$numberLong": "9007199254740993" }
}

This is why a MongoDB export does not look like plain JSON — those $oid and $date wrappers carry type information that standard JSON would lose.

Export a collection to JSON

Use mongoexport to write a collection to a JSON file:

# One JSON object per line (default)
mongoexport --db=mydb --collection=users --out=users.json

# A proper JSON array
mongoexport --db=mydb --collection=users \
  --jsonArray --out=users.json

# Only specific fields
mongoexport --db=mydb --collection=users \
  --fields=name,email --out=users.json

Note: without --jsonArray, the output is NDJSON — one object per line, not a single array. Many tools need the array form.

Format & inspect your MongoDB export

Paste a mongoexport file to pretty-print it, validate the structure, and read the Extended JSON clearly. Free and private.

Open tool →

Import JSON into MongoDB

# Import a JSON array
mongoimport --db=mydb --collection=users \
  --jsonArray --file=users.json

# Import NDJSON (one object per line)
mongoimport --db=mydb --collection=users --file=users.ndjson

Convert Extended JSON to plain JSON

If you need standard JSON for another system, you must strip the type wrappers — turning {"$oid": "..."} into a plain string. Drivers can do this with relaxed Extended JSON mode, or you can transform it in code:

// JavaScript example
const plain = JSON.parse(JSON.stringify(doc, (k, v) =>
  v && v.$oid ? v.$oid : (v && v.$date ? v.$date : v)
));

Querying with JSON

MongoDB queries are themselves JSON-like documents:

// Find active users over 30
db.users.find({ active: true, age: { $gt: 30 } })

// Project only specific fields
db.users.find({}, { name: 1, email: 1, _id: 0 })

Common gotchas

  • NDJSON vs array: remember --jsonArray on both export and import, or tools will choke.
  • ObjectId is not a string: querying _id needs ObjectId("..."), not the raw hex string.
  • Dates: Extended JSON $date wrappers are not standard — convert before sending to systems that expect plain JSON.
  • Number precision: large integers use $numberLong to avoid JavaScript's 53-bit precision limit.