This page follows the BSON specification version 1.1, read 2026-11-12, with the MongoDB ObjectID and Decimal128 specifications, read 2026-11-12. Numbers are little-endian except where noted.
Documents
- A document is a 4-byte total length (including itself and the final byte), the
elements, and a
00byte. - An element is a type byte, the field name as a NUL-terminated string of modified UTF-8 (cstring), and the value.
- A string value is a 4-byte length (bytes including the final NUL), the UTF-8
bytes and a
00. A cstring’s bytes must not contain00, so it is not full UTF-8; a string, being length-prefixed, has no such rule.
Element types
| Type | Name | Value |
|---|---|---|
01 | double | 8-byte IEEE 754 |
02 | string | string |
03 | embedded document | document |
04 | array | document whose keys are "0", "1", "2"… |
05 | binary | 4-byte length, subtype byte, the bytes |
06 | undefined | Deprecated; no value |
07 | ObjectId | 12 bytes; see below |
08 | boolean | 00 false, 01 true |
09 | UTC datetime | 8-byte signed milliseconds since 1970 |
0A | null | No value |
0B | regular expression | cstring pattern, cstring options in alphabetical order (the specification lists i case-insensitive, m multiline, s dotall, u Unicode \w, \W and so on, x verbose) |
0C | DBPointer | Deprecated: string and 12 bytes |
0D | JavaScript code | string |
0E | symbol | Deprecated: string |
0F | code with scope | Deprecated: 4-byte total length, string, document |
10 | 32-bit integer | 4-byte signed |
11 | timestamp | 8 bytes (uint64): the first 4 bytes are an increment, the second 4 a timestamp; an internal type used by MongoDB replication and sharding |
12 | 64-bit integer | 8-byte signed |
13 | decimal128 | 16-byte IEEE 754-2008 decimal; the coefficient is stored as a binary integer (BID), not densely packed decimal |
FF | min key | No value; compares lower than everything |
7F | max key | No value; compares higher than everything |
Binary subtypes
| Subtype | Meaning |
|---|---|
00 | Generic binary; the default for drivers and tools |
01 | Function |
02 | Binary (old): the data starts with its own 4-byte length again; deprecated in favour of 00 |
03 | UUID (old); deprecated in favour of 04 |
04 | UUID |
05 | MD5 |
06 | Encrypted BSON value |
07 | Compressed BSON column (delta, delta-of-delta and run-length encoding) |
08 | Sensitive |
09 | Vector: packed 1-bit unsigned, signed 8-bit integer or 32-bit float values, all of one type |
80–FF | User defined |
ObjectId
12 bytes: a 4-byte big-endian timestamp (unsigned seconds since the Unix epoch), a
5-byte random value generated once per process and unique to the machine and process,
and a 3-byte big-endian counter that starts from a random value and wraps from
FFFFFF to 000000. Originally the random value was a machine
ID and a process ID; the ObjectID specification does not give their sizes.
Example
16 00 00 00 document length 22
02 68 65 6C 6C 6F 00 type string, name "hello"
06 00 00 00 77 6F 72 6C 64 00 length 6, "world" and NUL
00 end of document