Home

BSONdocument

BSONdocument, commonly referred to as a BSON document, is a binary-encoded serialization of JSON-like documents used primarily in MongoDB and related ecosystems. It stores data in a compact, machine-friendly format, enabling efficient storage, transmission, and in-memory processing.

A BSON document consists of a 32-bit little-endian total size field, followed by a sequence of elements.

BSON supports a rich set of data types beyond JSON, including double, string, embedded document and array,

In practice, BSONdocument is the wire format and storage format used by MongoDB drivers and servers, permitting

Compared with JSON, BSONdocument is binary and type-rich, which reduces parsing overhead and preserves type information

Each
element
begins
with
a
single
byte
indicating
its
type,
followed
by
a
cstring
key
(UTF-8,
terminated
by
a
NUL
byte),
and
then
the
value
encoded
according
to
its
type.
The
document
ends
with
a
NUL
terminator.
Documents
may
be
nested
to
form
complex
structures.
binary
data,
ObjectId,
boolean,
date,
null,
regular
expression,
int32,
int64,
and
decimal128,
as
well
as
special
types
like
minKey
and
maxKey.
Some
legacy
types
such
as
undefined,
DBPointer,
and
symbol
exist
in
older
specifications.
Arrays
are
encoded
as
documents
with
numeric
keys.
fast
encoding/decoding
and
efficient
traversal.
In
MongoDB,
there
is
a
practical
maximum
document
size
of
16
megabytes,
though
larger
composite
data
structures
can
be
built
from
multiple
documents.
BSON
is
also
used
by
other
systems
and
libraries
that
require
a
binary,
schema-flexible
representation
of
data.
but
at
the
cost
of
human
readability.
It
is
designed
for
fast
machine
processing
and
compactness,
while
maintaining
compatibility
with
JSON-like
structures.
See
also
BSON,
MongoDB,
JSON,
and
binary
serialization
formats.