LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 914 min read

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

JSONBSON
FormatHuman-readable textBinary
Data typesString, Number, Boolean, null, Array, ObjectAll of JSON's types, plus Date, ObjectId, Decimal128, Binary, and more
Traversal speedRequires parsing the whole textEncodes field lengths, allowing faster skipping
Where you see itAPI responses, config filesMongoDB's actual storage and wire format

Core Data Types

TypeExampleNotes
String"Ada Lovelace"UTF-8 encoded text
Number (Int32/Int64/Double)42, 3.14MongoDB distinguishes integer and floating-point precision internally
Booleantrue / false
ObjectIdObjectId("64f1a2b3...")The default type for _id
DateISODate("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
Decimal128NumberDecimal("19.99")High-precision decimal, ideal for currency
NullnullRepresents an intentionally empty value
Use Decimal128 for Money

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

Storing dates as plain strings

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.

Using a plain Double for monetary values

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.

Next Lesson →

Creating Databases & Collections