BSON & Data Types
Understand BSON, the binary format MongoDB actually stores documents in, and the specific data types it supports.
What Is BSON?
BSON (Binary JSON) is the binary-encoded format MongoDB actually stores documents in internally, and it's what you interact with (mostly transparently) through the shell and drivers. It extends JSON with additional data types JSON itself doesn't have, and is designed to be fast to parse and traverse.
BSON vs JSON
| JSON | BSON | |
|---|---|---|
| Format | Human-readable text | Binary |
| Data types | String, Number, Boolean, null, Array, Object | All of JSON's types, plus Date, ObjectId, Decimal128, Binary, and more |
| Traversal speed | Requires parsing the whole text | Encodes field lengths, allowing faster skipping |
| Where you see it | API responses, config files | MongoDB's actual storage and wire format |
Core Data Types
| Type | Example | Notes |
|---|---|---|
| String | "Ada Lovelace" | UTF-8 encoded text |
| Number (Int32/Int64/Double) | 42, 3.14 | MongoDB distinguishes integer and floating-point precision internally |
| Boolean | true / false | |
| ObjectId | ObjectId("64f1a2b3...") | The default type for _id |
| Date | ISODate("2026-01-15T00:00:00Z") | Stored as milliseconds since the Unix epoch |
| Array | ["a", "b", "c"] | Can hold mixed types, including nested documents |
| Embedded Document | { "city": "London" } | A nested object, sharing the same document model |
| Decimal128 | NumberDecimal("19.99") | High-precision decimal, ideal for currency |
| Null | null | Represents an intentionally empty value |
Plain JavaScript numbers (and BSON's Double type) use floating-point representation, which can introduce tiny rounding errors — for currency and other values needing exact precision, use Decimal128 instead.
Common Beginner Mistakes
A string like "2026-01-15" can't be queried with date range operators or sorted correctly — always use the Date type for actual dates.
Floating-point rounding errors can silently corrupt financial calculations over time — use Decimal128 for anything involving money.
FAQs
Rarely for everyday use — the shell and drivers translate JavaScript/JSON values to and from BSON automatically; understanding it mainly helps with edge cases like precise number types and dates.
Technically yes, since MongoDB doesn't enforce types by default, but this is almost always a modeling mistake to avoid — consistent typing per field is a best practice even in a flexible-schema database.
Summary
BSON extends JSON with richer, more precise data types, and choosing the right type per field (especially Date and Decimal128) matters for correctness. Next, you'll create your first databases and collections hands-on.