Format:BSON
| Extensions | .bson |
|---|---|
| Endianness | little-endian |
| Website | https://bsonspec.org |
| On this wiki | Facts · Code · Notes · Topics |
MongoDB's binary JSON: a document is int32 total length, a list of elements (type byte, NUL-terminated key, value) and a 0x00 terminator; little-endian.
Layout
document = int32 total size (including itself and the trailing 0x00) + elements + 0x00. element = type byte + cstring key + value. Arrays are documents with keys "0","1",... No magic: check that the first int32 equals the file size (for .bson dumps, concatenated documents) and that the last byte is 0x00.
Type bytes
0x01 double (8); 0x02 string (int32 length incl. NUL, bytes, NUL); 0x03 embedded document; 0x04 array; 0x05 binary (int32 length, subtype byte, bytes; subtype 0x00 generic, 0x04 UUID); 0x06 undefined (deprecated); 0x07 ObjectId (12 bytes); 0x08 boolean (1 byte); 0x09 UTC datetime (int64 ms); 0x0A null; 0x0B regex (two cstrings); 0x0C DBPointer (deprecated); 0x0D JavaScript code; 0x0E symbol (deprecated); 0x0F code with scope; 0x10 int32; 0x11 timestamp (uint64); 0x12 int64; 0x13 decimal128 (16 bytes); 0xFF min key; 0x7F max key.
Sources
https://bsonspec.org/spec.html. The type byte table is from the spec and general knowledge; in https://github.com/mongodb/specifications source/bson-corpus/bson-corpus.md only types 0x01 (double), 0x05 (binary), 0x13 (Decimal128) and the 0x00 whole-document convention were read. Checked: every type byte above (0x01-0x13, 0x7F max key, 0xFF min key) and binary subtypes 0x00 and 0x04 match BSON_TYPE_* and BSON_SUBTYPE_* in mongodb/mongo-c-driver src/libbson/src/bson/bson-types.h @ 8ddb41367a. That header also names subtypes 0x01 function, 0x02 old binary, 0x03 old UUID, 0x05 MD5, 0x06 encrypted, 0x07 column, 0x08 sensitive, 0x09 vector and 0x80 user-defined.
Code & repositories
- https://github.com/mongodb/specifications @
7235b61bde1bc75e51f28d8f67dd935198969c02· license: CC-BY-NC-SA-3.0 (declared by the contributor; GitHub's detector did not recognize LICENSE.md)
MongoDB driver specifications incl. BSON corpus (documentation only; CC BY-NC-SA)
Tested on: source read, not run: read bson-corpus.md · checked 2026-10-18; last activity 2026-10-16