BSON types
corebeginnerBSON has more types than JSON: alongside string, boolean, null, object and array, it has distinct number types (double, int32, int64, decimal128), a real date type, ObjectId, binary data, regex, and a couple of rarely used legacy types.
Think of it as
JSON has one number type; BSON has four, because a database has to store what a number actually is — a 32-bit counter, a 64-bit ID, an exact decimal for money — not just "a number" the way a text format can get away with.
What we're doing: Show why storing money as a plain double instead of Decimal128 loses precision.
- 2
- double is binary floating point — it cannot represent every base-10 fraction exactly, the same limitation every language's float type has.
- 5
- decimal128 stores an exact base-10 value, so this addition has no rounding error at all.
Why this works: A double is fine for measurements and scores where a tiny rounding error is harmless. Money is exactly the case where it is not — small errors compound across many transactions, which is what decimal128 exists to prevent.
Storing every number as whatever the driver defaults to
Wrong
Better
What you see: Prices computed by summing many documents drift by fractions of a cent over time, and exact-equality comparisons on money fields intermittently fail.
Why: A driver's default numeric type is chosen for convenience, not correctness for every field — money specifically needs the exact type, decimal128, chosen deliberately rather than left to the default.
- JSON
- One generic "number"
- No fixed width
- No exact decimal type
- BSON
- double, int32, int64, decimal128
- Each has a fixed, known width
- decimal128 is exact for money
The BSON type list
Together
Remember: BSON has more types than JSON: double/int32/int64/decimal128 instead of one number, plus date, objectId, binary, regex. Use decimal128 for money.
See also: bson vs json · integer types and precision · decimal128

