BSON

BSON (Binary JSON) is the binary-encoded serialization format MongoDB uses to store and transmit documents. It extends JSON with additional data types (date, ObjectId, decimal128, binary data, regex), preserves field ordering, and is faster to parse and traverse than JSON text.

Extensions beyond JSON

  • ObjectId. 12-byte unique identifier embedding timestamp, machine, process, and counter.
  • Date. Native 64-bit integer milliseconds since the Unix epoch.
  • Decimal128. 128-bit IEEE 754-2008 decimal for exact financial arithmetic.
  • Binary data. Embed arbitrary bytes with a subtype tag (UUID, encrypted, image).
  • Long. 64-bit signed integers (JSON's Number is double-precision float, which loses precision past 2^53).
  • Regex, MinKey, MaxKey, Timestamp, JavaScript code. Niche but specified.

BSON vs JSON

  • JSON. Human-readable text; standard everywhere; loses precision for large integers and dates; no native binary type.
  • BSON. Binary; slightly larger on wire for short documents; richer type system; faster scan operations.
🔗

Subscribe to Sahil's Playbook

Clear thinking on product, engineering, and building at scale. No noise. One email when there's something worth sharing.
[email protected]
Subscribe
Mastodon