BSON_CONTEXT_T(3)

πŸ’… BSON_CONTEXT_T(3) β€” the libbson one, bestie

okay so this is the libbson manual page for BSON OID Generation Context and honestly it's way less scary than the name makes it sound. let's get into it 🫢


✨ SYNOPSIS ✨

like, the whole vibe in one code block:

#include <bson/bson.h>

typedef enum {
  BSON_CONTEXT_NONE = 0,
  BSON_CONTEXT_DISABLE_PID_CACHE = (1 << 2),
} bson_context_flags_t;

typedef struct _bson_context_t bson_context_t;

bson_context_t *
bson_context_get_default (void);
bson_context_t *
bson_context_new (bson_context_flags_t flags);
void
bson_context_destroy (bson_context_t *context);

πŸŽ€ DESCRIPTION πŸŽ€

so the bson_context_t struct is literally just the context used when you're generating BSON Object IDs β€” it's the little setup that lets you override the behavior of generating ObjectIDs. main character energy for something so small πŸ’…

now, the flags. only these three are actually used:

  • BSON_CONTEXT_NONE
  • BSON_CONTEXT_THREAD_SAFE
  • BSON_CONTEXT_DISABLE_PID_CACHE

the others? yeah they literally have no effect. dead. not invited to the party πŸ’€

no cap, only those three matter β€” don't come for me, that's just what the page says.

🌸 EXAMPLE 🌸

an actual working little program, copy it character for character bestie:

#include <bson/bson.h>

int
main (int argc, char *argv[])
{
   bson_context_t *ctx = NULL;
   bson_oid_t oid;

   /* use default context, via bson_context_get_default() */
   bson_oid_init (&oid, NULL);

   /* specify a local context for additional control */
   ctx = bson_context_new (BSON_CONTEXT_NONE);
   bson_oid_init (&oid, ctx);

   bson_context_destroy (ctx);

   return 0;
}

see how it starts with the default context (that's bson_context_get_default(), passed as NULL there πŸ‘€) and then also spins up a local one with bson_context_new (BSON_CONTEXT_NONE) for extra control? iconic. then it cleans up with bson_context_destroy (ctx) because we're responsible queens 🧚


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc


2009-present, MongoDB, Inc.


okay that's the whole page, go off and generate some ObjectIDs responsibly ⭐

libbson 2.5.6 Β· Oct 09, 2026 β€” mwah, that's the sign-off bestie, see you in the next manpage πŸ˜­πŸ¦‹

πŸ“¦ where this page comes from