BSON_T(3)

BSON Document Abstraction πŸ¦‹

okay bestie, welcome to the BSON_T(3) page from libbson. this is literally the whole vibe of BSON documents: the bson_t struct, a whole pile of BSON_APPEND_* macros, and the rules you gotta respect so your data stays iconic. no cap. πŸ’…

✨ SYNOPSIS ✨

          #include <bson/bson.h>

          /**
           * bson_empty:
           * @b: a bson_t.
           *
           * Checks to see if @b is an empty BSON document. An empty BSON document is
           * a 5 byte document which contains the length (4 bytes) and a single NUL
           * byte indicating end of fields.
           */
          #define bson_empty(b) /* ... */

          /**
           * bson_empty0:
           *
           * Like bson_empty() but treats NULL the same as an empty bson_t document.
           */
          #define bson_empty0(b) /* ... */

          /**
           * bson_clear:
           *
           * Easily free a bson document and set it to NULL. Use like:
           *
           * bson_t *doc = bson_new();
           * bson_clear (&doc);
           * BSON_ASSERT (doc == NULL);
           */
          #define bson_clear(bptr) /* ... */

          /**
           * BSON_MAX_SIZE:
           *
           * The maximum size in bytes of a BSON document.
           */
          #define BSON_MAX_SIZE /* ... */

          #define BSON_APPEND_ARRAY(b, key, val) \
             bson_append_array (b, key, (int) strlen (key), val)

          #define BSON_APPEND_ARRAY_BEGIN(b, key, child) \
             bson_append_array_begin (b, key, (int) strlen (key), child)

          #define BSON_APPEND_BINARY(b, key, subtype, val, len) \
             bson_append_binary (b, key, (int) strlen (key), subtype, val, len)

          #define BSON_APPEND_BINARY_UNINIT(b, key, subtype, val, len) \
             bson_append_binary_uninit (b, key, (int) strlen (key), subtype, val, len)

          #define BSON_APPEND_BOOL(b, key, val) \
             bson_append_bool (b, key, (int) strlen (key), val)

          #define BSON_APPEND_CODE(b, key, val) \
             bson_append_code (b, key, (int) strlen (key), val)

          #define BSON_APPEND_CODE_WITH_SCOPE(b, key, val, scope) \
             bson_append_code_with_scope (b, key, (int) strlen (key), val, scope)

          #define BSON_APPEND_DBPOINTER(b, key, coll, oid) \
             bson_append_dbpointer (b, key, (int) strlen (key), coll, oid)

          #define BSON_APPEND_DOCUMENT_BEGIN(b, key, child) \
             bson_append_document_begin (b, key, (int) strlen (key), child)

          #define BSON_APPEND_DOUBLE(b, key, val) \
             bson_append_double (b, key, (int) strlen (key), val)

          #define BSON_APPEND_DOCUMENT(b, key, val) \
             bson_append_document (b, key, (int) strlen (key), val)

          #define BSON_APPEND_INT32(b, key, val) \
             bson_append_int32 (b, key, (int) strlen (key), val)

          #define BSON_APPEND_INT64(b, key, val) \
             bson_append_int64 (b, key, (int) strlen (key), val)

          #define BSON_APPEND_MINKEY(b, key) \
             bson_append_minkey (b, key, (int) strlen (key))

          #define BSON_APPEND_DECIMAL128(b, key, val) \
             bson_append_decimal128 (b, key, (int) strlen (key), val)

          #define BSON_APPEND_MAXKEY(b, key) \
             bson_append_maxkey (b, key, (int) strlen (key))

          #define BSON_APPEND_NULL(b, key) bson_append_null (b, key, (int) strlen (key))

          #define BSON_APPEND_OID(b, key, val) \
             bson_append_oid (b, key, (int) strlen (key), val)

          #define BSON_APPEND_REGEX(b, key, val, opt) \
             bson_append_regex (b, key, (int) strlen (key), val, opt)

          #define BSON_APPEND_UTF8(b, key, val) \
             bson_append_utf8 (b, key, (int) strlen (key), val, (int) strlen (val))

          #define BSON_APPEND_SYMBOL(b, key, val) \
             bson_append_symbol (b, key, (int) strlen (key), val, (int) strlen (val))

          #define BSON_APPEND_TIME_T(b, key, val) \
             bson_append_time_t (b, key, (int) strlen (key), val)

          #define BSON_APPEND_TIMEVAL(b, key, val) \
             bson_append_timeval (b, key, (int) strlen (key), val)

          #define BSON_APPEND_DATE_TIME(b, key, val) \
             bson_append_date_time (b, key, (int) strlen (key), val)

          #define BSON_APPEND_TIMESTAMP(b, key, val, inc) \
             bson_append_timestamp (b, key, (int) strlen (key), val, inc)

          #define BSON_APPEND_UNDEFINED(b, key) \
             bson_append_undefined (b, key, (int) strlen (key))

          #define BSON_APPEND_VALUE(b, key, val) \
             bson_append_value (b, key, (int) strlen (key), (val))

          typedef struct {
             uint32_t flags;       /* Internal flags for the bson_t. */
             uint32_t len;         /* Length of BSON data. */
             uint8_t padding[120]; /* Padding for stack allocation. */
          } bson_t;

πŸ’– DESCRIPTION πŸ’–

okay so bson_t is a structure that represents a BSON document. It manages the underlying BSON encoded buffer, and for mutable documents it can append new data to the document. literally the little manager of your document’s whole arc. 🫢

πŸ”₯ PERFORMANCE NOTES πŸ”₯

bson_t tries to use an inline allocation inside the structure itself to speed up performance for small documents. Once that internal buffer has been exhausted, a heap allocated buffer will be dynamically allocated. That is why it is essential to call bson_destroy() on allocated documents. do not skip this bestie, it is literally essential. 😭

πŸ’€ DUPLICATE KEYS πŸ’€

the BSON specification allows BSON documents to have duplicate keys. Documents are stored as an ordered list of key-value pairs, and a bson_t may contain duplicate keys. but listen: applications should refrain from generating such documents, because MongoDB server behavior is undefined when a BSON document contains duplicate keys. that’s the warning, periodt. πŸ™ˆ

🌸 EXAMPLE 🌸

          static void
          create_on_heap (void)
          {
             bson_t *b = bson_new ();

             BSON_APPEND_INT32 (b, "foo", 123);
             BSON_APPEND_UTF8 (b, "bar", "foo");
             BSON_APPEND_DOUBLE (b, "baz", 1.23f);

             bson_destroy (b);
          }

πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc

2009-present, MongoDB, Inc.


and that’s the page, bestie! this lil BSON_T(3) manual page came from libbson version 2.5.6, dated Oct 09, 2026 🫢✨

πŸ“¦ where this page comes from