BSON_VALUE_T(3)

πŸ’… BSON Boxed Container Type πŸ’…

okay so bestie, welcome to bson_value_t from libbson β€” yes, that libbson, the C library one, and yes, this page is gonna be your new personality trait ✨


✨ SYNOPSIS ✨

#include <bson/bson.h>

typedef struct _bson_value_t {
   bson_type_t value_type;
   union {
      bson_oid_t v_oid;
      int64_t v_int64;
      int32_t v_int32;
      int8_t v_int8;
      double v_double;
      bool v_bool;
      int64_t v_datetime;
      struct {
         uint32_t timestamp;
         uint32_t increment;
      } v_timestamp;
      struct {
         uint32_t len;
         char *str;
      } v_utf8;
      struct {
         uint32_t data_len;
         uint8_t *data;
      } v_doc;
      struct {
         uint32_t data_len;
         uint8_t *data;
         bson_subtype_t subtype;
      } v_binary;
      struct {
         char *regex;
         char *options;
      } v_regex;
      struct {
         char *collection;
         uint32_t collection_len;
         bson_oid_t oid;
      } v_dbpointer;
      struct {
         uint32_t code_len;
         char *code;
      } v_code;
      struct {
         uint32_t code_len;
         char *code;
         uint32_t scope_len;
         uint8_t *scope_data;
      } v_codewscope;
      struct {
         uint32_t len;
         char *symbol;
      } v_symbol;
      bson_decimal128_t v_decimal128;
   } value;
} bson_value_t;

like, that union is literally the whole squad assembled β€” every single one of these members is showing up to the group chat: v_oid, v_int64, v_int32, v_int8, v_double, v_bool, v_datetime, v_timestamp, v_utf8, v_doc, v_binary, v_regex, v_dbpointer, v_code, v_codewscope, v_symbol, v_decimal128. no one left behind, periodt 🫢


🌸 DESCRIPTION 🌸

so the bson_value_t structure is a boxed type β€” and before you ask, no, not cardboard box, more like a cute lil gift-wrapped container 🎁 it's for encapsulating a type that gets figured out at runtime, aka the type doesn't announce itself at compile time, it just shows up and reveals its whole identity later. main character energy frankly πŸ’…


πŸ¦‹ EXAMPLE πŸ¦‹

here's the vibe check in action:

const bson_value_t *value;

value = bson_iter_value (&iter);

if (value->value_type == BSON_TYPE_INT32) {
   printf ("%d\n", value->value.v_int32);
}

see how it peeks at value_type first to find out what it's dealing with, then reaches into the matching union member? iconic. no cap, that's the whole move 😌


πŸŽ€ AUTHOR πŸŽ€

MongoDB, Inc


2009-present, MongoDB, Inc.


and that's the page, bestie πŸ’– before you close the tab β€” this absolute classic is from libbson version 2.5.6, dated Oct 09, 2026 🌈 go forth and box those types, you've got this πŸ§šπŸ’œ

πŸ“¦ where this page comes from