BSON_APPEND_DOCUMENT(3)

πŸ’… BSON_APPEND_DOCUMENT(3) β€” the libbson glow-up

okay bestie, so this is a page straight out of libbson and it's genuinely one of the cutest little functions ever. it appends a document inside another document. nesting!! we love to see it. πŸͺ†βœ¨


🌸 SYNOPSIS 🌸

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

bool
bson_append_document (bson_t *bson,
                      const char *key,
                      int key_length,
                      const bson_t *value);

so you've got two entry points, iykyk: the macro BSON_APPEND_DOCUMENT which is literally just a lil shortcut that calls strlen on your key for you (so your key has to be a NUL-terminated C string, no funny business), and the real function bson_append_document where you pass the length yourself. main character energy. πŸ‘‘


πŸŽ€ PARAMETERS πŸŽ€

  • bson β€” a bson_t. this is the doc you're stuffing stuff into. the host. the parent. the mother if you will. 🫢
  • key β€” an ASCII C string holding the name of the field. keep it ASCII, bestie, that's the assignment. ✨
  • key_length β€” the length of key in bytes. or, and this is the fun part, pass -1 and it'll figure out the length itself with strlen(). lowkey magical. πŸͺ„
  • value β€” a bson_t containing the sub-document you wanna append. the child!! the baby!! 🐣

πŸ¦‹ DESCRIPTION πŸ¦‹

bson_append_document() shall append child to bson using the specified key. and like β€” the type of the field? it's gonna be a document. that's it. that's the vibe. a document inside a document, no cap. πŸ’–


πŸ”₯ RETURNS πŸ”₯

returns true if the operation was applied successfully. slay. βœ…

but β€” and listen, this is important β€” the function will fail if appending value grows bson larger than INT32_MAX. 😭 that's a hard limit, not a suggestion, so don't come crying to me when your mega-doc doesn't fit. check your return value, fr fr.


πŸ’œ AUTHOR πŸ’œ

MongoDB, Inc β€” the icons who brought you this whole thing. πŸ‘‘


2009-present, MongoDB, Inc. 🫑


and that's the whole page, bestie!! go forth and nest those documents responsibly πŸ’…βœ¨

β€” libbson version 2.5.6, dated Oct 09, 2026 πŸŽ€ mwah, ily, bye!! 🫢

πŸ“¦ where this page comes from