Understanding BSON and Its Relationship with JSON
MongoDB stores documents using BSON (Binary JSON), a binary-encoded serialization format designed to represent structured data efficiently. BSON builds on a JSON-like document model but supports additional data types and explicit type distinctions.
Understanding BSON is essential because MongoDB does not treat every value as an ordinary JavaScript value or JSON string. The type of a stored value influences matching, sorting, arithmetic, validation, and serialization.
JSON versus BSON
JSON supports objects, arrays, strings, numbers, Booleans, and null. BSON additionally supports types such as ObjectId, Date, Decimal128, binary data, and other specialized values.
Consider these documents:
{
name: "Arun",
age: 28,
active: true
}
{
name: "Arun",
age: NumberInt(28),
active: true,
createdAt: ISODate("2026-10-22T10:00:00Z"),
_id: ObjectId("65a000000000000000000001")
}
The second example uses MongoDB shell notation to demonstrate BSON types. NumberInt(), ISODate(), and ObjectId() are shell helpers, not standard JSON syntax.
Why BSON matters
1. Type-aware queries
MongoDB distinguishes numbers from strings. A field containing the string "28" is not equivalent to a field containing the number 28 in an ordinary equality or numeric range query.
2. Specialized values
Dates should generally be stored as BSON Dates when date comparisons and date arithmetic are required. Monetary values requiring exact decimal representation may benefit from Decimal128 or integer minor units.
3. Document serialization
Applications may transform BSON documents into JSON before returning them through an HTTP API. This transformation can change how values are represented. For example, an ObjectId may become a string, and a BSON Date commonly becomes an ISO-formatted string.
4. Storage and size
BSON has its own encoding rules and overhead. The size of a BSON document is not necessarily the same as the number of characters in its JSON representation.
MongoDB's maximum BSON document size is 16 MiB. This constraint matters when storing large arrays, embedded documents, or binary data.
BSON does not mean every query is binary
Developers generally write queries using MongoDB's document-shaped syntax. The driver serializes supported application values into BSON when communicating with the server.
For example:
db.users.find({ age: { $gte: 18 } });
You write a readable query expression. MongoDB evaluates the filter using its query semantics and stored BSON values.
Practical rule
Choose a type based on the meaning of the data, not merely on how the value looks when printed. A date string may look like a date, but it is not a BSON Date. A numeric string may contain digits, but it is not a BSON number.
Text-based data interchange formatBinary-encoded document representationImportant for query behavior16 MiB- MongoDB uses BSON to represent documents.
- BSON supports types beyond standard JSON.
- The stored BSON type affects query and validation behavior.
- BSON document size is not the same as JSON text length.
- MongoDB documents have a maximum BSON size of 16 MiB.
What is the most important difference between the values `28` and `"28"` in MongoDB?