BSON vs JSON
Understand what BSON is, how it differs from JSON, and why MongoDB uses it internally.
BSON vs JSON
When you insert a document into MongoDB, you write it as a JavaScript object — which looks exactly like JSON. But MongoDB does not actually store it as JSON. It converts it to BSON first.
Understanding the difference between JSON and BSON helps you understand why MongoDB supports types that plain JSON does not — like Date, ObjectId, and Int32.
What is JSON?
JSON stands for JavaScript Object Notation. It is a text format for representing data. It is human-readable, widely used, and supported by almost every programming language.
A JSON document looks like this:
{
"name": "Ali Hassan",
"age": 16,
"enrolled": true,
"subjects": ["Math", "Physics"],
"address": {
"city": "Lahore",
"country": "Pakistan"
}
}JSON supports only these types:
| JSON Type | Example |
|---|---|
| String | "Ali Hassan" |
| Number | 16, 3.75 |
| Boolean | true, false |
| Array | ["Math", "Physics"] |
| Object | { "city": "Lahore" } |
| Null | null |
That is it. JSON has no Date type, no ObjectId type, no way to tell apart an integer from a decimal number. Everything is either a string, a number, or one of the others above.
What is BSON?
BSON stands for Binary JSON. It is the format MongoDB uses to store documents internally. BSON takes the same structure as JSON but encodes it as binary — ones and zeros — instead of plain text.
MongoDB developed BSON specifically to solve the limitations of JSON. It adds more data types, makes data faster to scan, and stores it more efficiently.
Here is the same student document as BSON — this is what it looks like internally:
\x8c\x00\x00\x00
\x02name\x00\x0b\x00\x00\x00Ali Hassan\x00
\x10age\x00\x10\x00\x00\x00
\x08enrolled\x00\x01
...
\x00You never read or write BSON directly. MongoDB handles the conversion automatically. You write JavaScript objects, MongoDB converts them to BSON when saving and converts them back when reading.
JSON vs BSON — Side by Side
You work with plain JavaScript objects. The conversion happens automatically behind the scenes.
Why BSON Instead of JSON?
More data types
This is the biggest reason. BSON supports types that JSON does not:
| Type | In JSON | In BSON |
|---|---|---|
| Date | ❌ No native type — stored as string | ✅ Native Date type |
| Integer | ❌ All numbers are the same | ✅ Int32, Int64 |
| Decimal | ❌ All numbers are the same | ✅ Double, Decimal128 |
| ObjectId | ❌ Not supported | ✅ Native ObjectId |
| Binary data | ❌ Not supported | ✅ BinData |
| Regular expression | ❌ Not supported | ✅ Native Regex |
Because BSON has a native Date type, MongoDB can store and query dates correctly — sort by date, filter by date range, and so on. If MongoDB used plain JSON, dates would just be strings and none of that would work reliably.
Faster to scan
BSON stores the length of each field at the beginning of that field. This means MongoDB can skip over fields it does not need without reading them — making queries faster.
With plain JSON text, to find a field at the end of a document, you have to read every character before it.
Lightweight
BSON encodes data as binary which is more compact than plain text in many cases — especially for numbers and binary data.
Types BSON Adds to Your Documents
Here are the extra BSON types you will actually use in MongoDB:
ObjectId — the unique _id MongoDB creates for every document:
_id: ObjectId("64a1f2c3e4b0a1b2c3d4e5f6")Date — a real date with a timestamp, not a string:
enrollmentDate: new Date("2024-09-01")Int32 — an explicit 32-bit integer:
rollNumber: Int32(1021)Int64 (Long) — for very large whole numbers:
totalMilliseconds: Long("9007199254740991")Decimal128 — for precise decimal numbers like money:
feeAmount: Decimal128("1500.50")BinData — for storing raw binary like images or files (though for large files, there are better options):
profilePhoto: BinData(0, "base64encodeddata...")Does This Change How You Write Code?
Mostly no. You write plain JavaScript objects and the MongoDB driver handles the BSON conversion for you. The only times you need to think about BSON are:
When using special types — if you need an ObjectId or a Date, you use the constructor:
const { ObjectId } = require('mongodb');
// Querying by _id requires ObjectId — not a plain string
db.students.findOne({ _id: new ObjectId("64a1f2c3e4b0a1b2c3d4e5f6") })When storing dates — always use new Date(), not a string:
// Correct — stored as BSON Date
enrollmentDate: new Date("2024-09-01")
// Wrong — stored as a plain string, date queries will not work
enrollmentDate: "2024-09-01"When you need precise numbers — for financial data, use Decimal128:
const { Decimal128 } = require('mongodb');
db.fees.insertOne({
studentName: "Ali Hassan",
amount: Decimal128("1500.50")
})Quick Summary
| JSON | BSON | |
|---|---|---|
| Format | Plain text | Binary |
| Human readable | ✅ Yes | ❌ No |
| Data types | 6 basic types | Many more types |
| Date support | ❌ No native type | ✅ Yes |
| ObjectId support | ❌ No | ✅ Yes |
| Speed | Slower to scan | Faster to scan |
| Used by MongoDB | For input/output | For internal storage |
You will never write BSON directly. Think of it as MongoDB's internal language — it speaks JavaScript with you and BSON with its storage engine. The conversion is automatic and invisible. The only thing you need to remember is to use the right constructors — new Date(), new ObjectId() — when working with special types.