Specification Version 1.1

BSON is a binary format in which zero or more ordered key/value pairs are stored as a single entity. We call this entity a document.

The following grammar specifies version 1.1 of the BSON standard. We've written the grammar using a pseudo-BNF syntax. Valid BSON data is represented by the document non-terminal.

Basic Types

The following basic types are used as terminals in the rest of the grammar. Each type must be serialized in little-endian format.

byte 1 byte (8-bits)
signed_byte(n) 8-bit, two's complement signed integer for which the value is n.
unsigned_byte(n) 8-bit unsigned integer for which the value is n.
int32 4 bytes (32-bit signed integer, two's complement)
int64 8 bytes (64-bit signed integer, two's complement)
uint64 8 bytes (64-bit unsigned integer)
double 8 bytes (64-bit IEEE 754-2008 binary floating point)
decimal128 16 bytes (128-bit IEEE 754-2008 decimal floating point)

Non-terminals

The following specifies the rest of the BSON grammar. Note that we use the * operator as shorthand for repetition (e.g. (byte*2) is byte byte). When used as a unary operator, * means that the repetition can occur 0 or more times.

document ::= int32 e_list unsigned_byte(0) BSON Document. int32 is the total number of bytes constituting the document.
e_list ::= element e_list
| ""
element ::= signed_byte(1) e_name double 64-bit binary floating point
| signed_byte(2) e_name string UTF-8 string
| signed_byte(3) e_name document Embedded document
| signed_byte(4) e_name document Array. See below for more information.
| signed_byte(5) e_name binary Binary data
| signed_byte(6) e_name Undefined (value). Deprecated.
| signed_byte(7) e_name (byte*12) ObjectId
| signed_byte(8) e_name unsigned_byte(0) Boolean (false)
| signed_byte(8) e_name unsigned_byte(1) Boolean (true)
| signed_byte(9) e_name int64 UTC datetime. int64 is UTC milliseconds since the Unix epoch.
| signed_byte(10) e_name Null value
| signed_byte(11) e_name cstring cstring Regular expression. See below for more information.
| signed_byte(12) e_name string (byte*12) DBPointer. Deprecated.
| signed_byte(13) e_name string JavaScript code
| signed_byte(14) e_name string Symbol. Deprecated.
| signed_byte(15) e_name code_w_s JavaScript code with scope. Deprecated.
| signed_byte(16) e_name int32 32-bit integer
| signed_byte(17) e_name uint64 Timestamp. A special internal type used by MongoDB replication and sharding. The first 4 bytes are an increment and the second 4 bytes are a timestamp.
| signed_byte(18) e_name int64 64-bit integer
| signed_byte(19) e_name decimal128 128-bit decimal floating point
| signed_byte(-1) e_name Min key. A special type that compares lower than all other possible BSON element values.
| signed_byte(127) e_name Max key. A special type that compares higher than all other possible BSON element values.
e_name ::= cstring Key name
string ::= int32 (byte*) unsigned_byte(0) String. The int32 is the number of bytes in the (byte*) plus one for the trailing null byte. The (byte*) is zero or more UTF-8 encoded characters.
cstring ::= (byte*) unsigned_byte(0) Zero or more modified UTF-8 encoded characters followed by the null byte. Because the (byte*) MUST NOT contain unsigned_byte(0), it is not full UTF-8.
binary ::= int32 subtype (byte*) BSON binary or BinData. An array of bytes, similar to a Java ByteArray. The int32 is the number of bytes in the (byte*). The subtype indicates the kind of data in the byte array.
subtype ::= unsigned_byte(0) Generic binary subtype. This is the most commonly used binary subtype and should be the 'default' for drivers and tools.
| unsigned_byte(1) Function
| unsigned_byte(2) Binary (old). Deprecated in favor of subtype 0. Drivers and tools should properly handle this subtype. See below for more information.
| unsigned_byte(3) UUID (old). Deprecated in favor of subtype 4. Drivers and tools for languages with a native UUID type should properly handle subtype 3.
| unsigned_byte(4) UUID
| unsigned_byte(5) MD5
| unsigned_byte(6) Encrypted BSON value
| unsigned_byte(7) Compressed BSON column. See below for more information.
| unsigned_byte(8) Sensitive
| unsigned_byte(9) Vector. See below for more information.
| unsigned_byte(128)—unsigned_byte(255) User-defined subtypes
code_w_s ::= int32 string document Code with scope. Deprecated. The int32 is the length in bytes of the entire code_w_s value. The string is JavaScript code. The document is a mapping from identifiers to values, representing the scope in which the string should be evaluated.

More Information

Type Description
Array A BSON document with integer values for the keys, starting at 0 and continuing sequentially. For example, the array ['red', 'blue'] encodes as the document {'0': 'red', '1': 'blue'}.
Regular expression

The first cstring is the regex pattern. The second cstring is the regex options string.

Options are identified by characters, which must be stored in alphabetical order. Regular expressions support the following options:

  • i enables case-insensitive matching
  • m enables multiline matching
  • s enables dotall mode ("." matches everything)
  • x enables verbose mode
  • u enables "\w", "\W", etc. to match Unicode
Binary (old) The structure of the binary data must be an int32 followed by a byte*. The int32 is the number of bytes in the repetition.
Compressed BSON column This data type uses delta and delta-of-delta compression and run-length-encoding for efficient element storage. It also has an encoding for sparse arrays containing missing values.
Vector

Dense array of numeric values stored in a binary format efficient for storage and retrieval. Vectors are effectively used to represent data in artificial intelligence, machine learning, semantic search, computer vision, and natural language processing applications.

All values within the vector must be of the same type. Vectors support the following types:

  • Packed binary (1-bit unsigned int)
  • Signed 8-bit integer (int)
  • 32-bit floating point (float)

For more information about BSON vectors, see the BSON Binary Subtype 9 specification document.