BSON
By MongoDB, Inc.
BSON (Binary JSON) is a binary-encoded serialization format that represents the same document-like data structures as JSON, extended with additional types such as dates, binary data, and specific numeric precisions. It was designed…
Definition
BSON (Binary JSON) is a binary-encoded serialization format that represents the same document-like data structures as JSON, extended with additional types such as dates, binary data, and specific numeric precisions. It was designed primarily as the internal storage and wire format for MongoDB, aiming for fast encoding and decoding along with efficient traversal of embedded fields without fully parsing a document.
Overview
BSON was created to address limitations JSON has as a storage format for a document database: JSON's text-based representation is straightforward to read but comparatively slow to parse, has no native binary or date types, and cannot express numeric precision distinctions that a database needs to store accurately. MongoDB introduced BSON as a binary alternative that keeps JSON's flexible, schema-less document model while adding the type fidelity and traversal efficiency a database engine needs. Structurally, a BSON document is a length-prefixed sequence of encoded fields, where each field carries an explicit type byte, a name, and a binary-encoded value, letting a parser skip over fields it does not need or jump directly to a nested subdocument without scanning surrounding text, something a text-based JSON parser cannot do without walking the whole string. BSON also embeds the total byte length of documents and strings up front, which lets an application allocate buffers and traverse structures more predictably than variable-length text parsing allows. BSON differs from JSON in the type system it supports: alongside the object, array, string, number, and boolean types JSON offers, BSON adds a native date type, a binary blob type, distinct 32-bit and 64-bit integer types, and MongoDB's ObjectId type used for document identifiers. This makes BSON strictly richer in type expression than plain JSON, but also means BSON documents cannot always be losslessly converted back to and from JSON without a translation layer that maps these extra types onto JSON conventions. In practice, BSON is used almost exclusively within and around the MongoDB ecosystem: it is how MongoDB stores documents on disk, how the driver protocol communicates queries and results between client applications and the database, and how tools like `mongodump` capture backups in a format that preserves full type fidelity. Application developers rarely handle raw BSON bytes directly; MongoDB drivers translate between BSON and native language objects automatically. BSON's binary encoding is not necessarily more compact than JSON for every document shape, since its length-prefixing and type bytes add overhead that can offset savings on smaller documents, so it should not be assumed to always beat gzip-compressed JSON on raw size. It is also tightly coupled to the MongoDB ecosystem rather than being a general-purpose interchange format the way Protocol Buffers or Avro are, so it sees little independent use outside MongoDB-adjacent tooling. Developers evaluating a serialization format for a new cross-language project would typically look at Protocol Buffers or Avro instead, reserving BSON specifically for cases already committed to MongoDB as the underlying data store.
Key Concepts
- Binary encoding of JSON-like documents with richer native types
- Length-prefixed fields allow fast traversal without full parsing
- Adds date, binary blob, and precise numeric types beyond JSON
- Serves as MongoDB's on-disk storage and wire protocol format
- Includes MongoDB's ObjectId type for document identifiers
- Not always more compact than compressed JSON for small documents
- Tightly integrated with MongoDB drivers across many languages
- Supports embedded subdocuments and arrays natively