BSON_NEW_FROM_DATA(3)

πŸ’… BSON_NEW_FROM_DATA πŸ’…

okay bestie, so this one is from libbson β€” the bson_new_from_data(3) page β€” and it's honestly pretty straightforward, so let's get into it ✨


🌸 SYNOPSIS 🌸

bson_t *
bson_new_from_data (const uint8_t *data, size_t length);

that's the whole signature, no cap. two args, one bson_t * coming back at you 🫢


πŸŽ€ PARAMETERS πŸŽ€

  • data β€” a BSON encoded document buffer. like, the actual bytes, sitting there in memory πŸ’–
  • length β€” the length of data in bytes. the size, the count, the how-much πŸ¦‹

✨ DESCRIPTION ✨

so bson_new_from_data() shall create a new bson_t on the heap and copy the contents of data right into it. allocated on the heap, contents copied, all of it πŸ’…

it "may be helpful when working with language bindings" β€” that's the intended vibe, iykyk β€” but ngl it's generally expected to be slower. the copy is doing copy things. it's giving convenient but not free 😭

🚨 important vibe check: it is an error if length does not match the length embedded in the first four bytes of the BSON data in data. like, those have to agree. the first four bytes of data literally encode a length, and if your length argument disagrees with it, that's an error. no negotiation on this one πŸ”₯

⭐ RETURNS ⭐

a newly allocated bson_t if successful β€” otherwise NULL. simple as that. slay when it works, NULL when it doesn't πŸ’€


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc β€” they wrote it, we're thanking them πŸ’œ


2009-present, MongoDB, Inc. β€” the full notice, untouched, as it should be πŸŽ€


so yeah, that's the whole page 🧚 memcpy energy, heap allocation energy, and one very specific error condition you should not sleep on. fr fr, make sure your length matches those first four bytes or it's not gonna be a good time πŸ“

later bestie, and happy BSON-ing!! πŸ¦‹ πŸ’–

β€” aka this is from libbson 2.5.6, dated Oct 09, 2026, and that's the whole sign-off ✨

πŸ“¦ where this page comes from