Introduction
BSON is the binary form in which MongoDB stores and sends its documents. It holds the same structure as JSON, objects and arrays of values, and adds types JSON lacks, such as dates, 32-bit and 64-bit integers, decimals, raw binary and MongoDB’s 12-byte ObjectId.
Unlike most compact formats, BSON puts the length in front of every document and
string, so a reader can skip over values it does not need. That makes it fast to scan
but often larger than JSON. The mongodump tool writes each collection as
a .bson file.
Structure
All numbers are little-endian. A document is a 32-bit total length, then its elements, then a zero byte. Each element is a type byte, the field name as a zero-terminated string, and the value. Common types:
| Type | Value |
|---|---|
01 | 64-bit floating point number |
02 | string: a 32-bit length, UTF-8 bytes and a zero byte |
03 | embedded document |
04 | array, stored as a document with the keys 0, 1, 2… |
05 | binary data: a length, a subtype byte, then the bytes |
07 | ObjectId, 12 bytes |
08 | boolean, one byte |
09 | date and time: milliseconds since 1970 as a 64-bit integer |
0A | null |
10, 12 | 32-bit and 64-bit integers |
13 | 128-bit decimal |
A .bson file from mongodump is simply one document after
another, with no header. The element
types page gives every type and subtype.
Not described
The reference follows bsonspec.org 1.1 and the MongoDB BSON specifications.
Known, not yet covered
- Viewer: the explorer opens a
.bsonfile of one document or of many in a row (a mongodump file), walks it once without recursion to find every document, element and problem, and shows the structure down to every byte: the length, each element's type byte, key and value (strings as length, characters and 00; binary as length, subtype and data; ObjectId as timestamp, random value and counter; regular expressions, timestamps, code with scope, DBPointers), embedded documents and arrays to any depth, and the terminator. The Elements table lists every element of the file in pages with its path (a.b.0), and the Documents table every document. Every element type is decoded, including Decimal128 (the BID layout, written as the specification's to-string rules do). The reader is checked against the MongoDB bson-corpus: all 735 valid documents (canonical and degenerate forms) decode to exactly the corpus's Canonical Extended JSON, and all 75 decode-error documents are reported as breaking the format. Notes are given for deprecated types, array keys that are not 0, 1, 2…, UUIDs and MD5s that are not 16 bytes, regular expression options that are out of order, and a key used twice in one document (the BSON specification says nothing either way and MongoDB's drivers differ, so the note says that and does not call it an error). Three binary subtypes are decoded as their specifications define. Vector (subtype 09): the dtype, the padding and every element (INT8, FLOAT32, and PACKED_BIT as bytes and as the bit string); a padding that is not 0 (or 0 to 7 for PACKED_BIT), an empty PACKED_BIT vector with padding, and a FLOAT32 length that is not a multiple of 4 break the format, as the specification's validation rules say, and non-zero ignored bits are a note; checked against the specification's own test corpus (int8, float32 and packed_bit: every valid vector decodes to its numbers and re-encodes to its canonical BSON, every invalid one with a BSON form is reported). Encrypted value (subtype 06): the blob subtype byte, then for an intent-to-encrypt marking its document (checked for v, a, and ki or ka) and for ciphertext the 16-byte key UUID, the original BSON type and the ciphertext length (the ciphertext itself is encrypted and is not decoded); a missing key UUID, an original type that is no BSON type, an empty ciphertext and a marking missing a required key are reported. Compressed column (subtype 07): the literals and Simple-8b runs (delta, delta of delta, run-length words, extended selectors, scale indexes for doubles, 128-bit strings, binary and Decimal128, ObjectId, datetime, timestamp, boolean, int32 and int64) are decoded to their values, with missing values marked; a bad control byte, a cut run, an invalid selector, a delta the type does not allow, bytes after the final 00 and the like are reported as notes (the document stays valid BSON, as the corpus's valid document with arbitrary subtype 07 bytes requires). Not covered: the values of a column in interleaved mode (control bytes F0 to F2) are not decoded, only its reference object is read and its streams counted (the order in which the streams' words are interleaved is derived from how many values each word holds, and that reader is not written); a column stops after 100,000 values with a note; the vector reader is checked against the specification's corpus, but the column and encrypted readers have no corpus (the column from the server source's code, the encrypted value from its specification), so they are checked against files built by hand from those descriptions, not against MongoDB's output; the Queryable Encryption blob subtypes after 0, 1 and 2 are not described by the specifications read and are shown as bytes with a note; and the problem list keeps the first 100,000 problems (the count is every one).
Unknown
- Reference: the compressed column (binary subtype 7) has no
specification in the mongodb/specifications repository. The
types page follows the public server
source; the detailed format document the source points to (a Google Docs file) and the
encoder (
bsoncolumnbuilder.cpp, which decides when strings and binary data become literals) were not opened. The viewer's column reader follows the same source (simple8b.h,simple8b.inl,simple8b_helpers.handsimple8b_type_util.cpp, read 2026-11-19), and run-length words repeat the last stored number of the previous block (the delta), not the last decoded value, assimple8b.inlkeeps it; no output of MongoDB's own decoder was available to compare. - Reference: the two MongoDB documents on the encrypted value disagree
about the ciphertext layout: BSON Binary Encrypted
(
bson-binary-encrypted/binary-encrypted.md) ends the blob with the ciphertext, while the appendix of the Client Side Encryption specification shows a 4-byteciphertext_lengthbefore it. The viewer follows the first (the ciphertext is the rest of the blob) and does not decode the Queryable Encryption blob subtypes, which neither document describes. - Reference: IEEE 754-2008 is a paid standard and was not opened; the Decimal128 layout rests on the MongoDB specification, the C driver source and IBM's decimal encoding document.
Links
Software
- MongoDB Database Tools –
mongodump,mongorestore, andbsondump, which prints BSON as JSON (Windows, macOS, Linux) - MongoDB Compass – MongoDB’s graphical client for browsing documents (Windows, macOS, Linux)
Developers
Reference
- BSON specification – the full list of element types, on bsonspec.org