BSON_ITER_INIT_FROM_DATA(3)
π BSON_ITER_INIT_FROM_DATA π
okay so bestie, welcome to the libbson manual page for bson_iter_init_from_data(3) β yes that's the manual name, it's giving "long function name energy" and honestly? iconic. this one's about booting up an iterator straight from a raw buffer of bytes, and it's lowkey one of the most useful little functions in the whole iterator family. let's get into it π«Ά
β¨ SYNOPSIS β¨
bool
bson_iter_init_from_data (bson_iter_t *iter, const uint8_t *data, size_t length);
like, that's the whole signature. three args, one bool back. no cap, that's it π
π PARAMETERS π
iterβ abson_iter_t. this is the iterator you're waking up, the main character of this whole operation πdataβ a buffer to initialize with. this is where your bytes live, so hand it over πlengthβ the length ofdatain bytes. yes bestie, you gotta tell it how big the buffer is, it can't read your mind π
π¦ DESCRIPTION π¦
so the bson_iter_init_from_data() function shall initialize iter to iterate upon the buffer data, which β and this is the important part β must contain a BSON document. like, that's non-negotiable, that's the vibe check, the buffer has to actually be a BSON doc or this whole thing is not it π
upon initialization, iter is placed before the first element. not on it. before it. so don't come for it when nothing's returned yet, it's literally positioned pre-first-element by design β¨
so what do you do next? callers must call bson_iter_next(), bson_iter_find(), or bson_iter_find_case() to advance to an element. pick your fighter:
bson_iter_next()β just walk forward, one at a timebson_iter_find()β hunt for an exact key namebson_iter_find_case()β same hunt, case-insensitive, be free π
πΈ RETURNS πΈ
returns true if the iter was successfully initialized. that's it, that's the return. no extra lore π
β EXAMPLE β
here's a whole little function that pulls the _id out of a document and prints it as a string, and honestly it ate:
static void
print_doc_id (const uint8_t *data, size_t length)
{
bson_iter_t iter;
bson_oid_t oid;
char oidstr[25];
if (bson_iter_init_from_data (&iter, data, length) && bson_iter_find (&iter, "_id") &&
BSON_ITER_HOLDS_OID (&iter)) {
bson_iter_oid (&iter, &oid);
bson_oid_to_string (&oid, oidstr);
printf ("%s\n", oidstr);
} else {
printf ("Document is missing _id.\n");
}
}
walk through it with me: init from data, find "_id", check BSON_ITER_HOLDS_OID(), then grab it with bson_iter_oid(), stringify with bson_oid_to_string() into oidstr[25], and printf it. and if any of those checks fail? you get Document is missing _id. β blunt, honest, we respect it π
π§ AUTHOR π§
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
okay bestie that's the whole page, go forth and iterate π¦β¨
cute little sign-off: this waslibbsonversion2.5.6, datedOct 09, 2026β see you in the next manpage, mwah ππ
π¦ where this page comes from
- epel 10: libbson-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms