BSON_ITER_T(3)
π BSON Document Iterator π
okay bestie, welcome to libbson's iterator page β this is the little struct that lets you walk through a BSON document element by element, and honestly? it's iconic. let's get into it π
β¨ SYNOPSIS β¨
first, the include (mandatory, no skipping):
#include <bson/bson.h>
then the whole family of "hey what type is this field actually" macro checks β every single one of them, here we go:
#define BSON_ITER_HOLDS_DOUBLE(iter) /* ... */
#define BSON_ITER_HOLDS_UTF8(iter) /* ... */
#define BSON_ITER_HOLDS_DOCUMENT(iter) /* ... */
#define BSON_ITER_HOLDS_ARRAY(iter) /* ... */
#define BSON_ITER_HOLDS_BINARY(iter) /* ... */
#define BSON_ITER_HOLDS_VECTOR(iter) /* ... */
#define BSON_ITER_HOLDS_VECTOR_INT8(iter) /* ... */
#define BSON_ITER_HOLDS_VECTOR_FLOAT32(iter) /* ... */
#define BSON_ITER_HOLDS_VECTOR_PACKED_BIT(iter) /* ... */
#define BSON_ITER_HOLDS_UNDEFINED(iter) /* ... */
#define BSON_ITER_HOLDS_OID(iter) /* ... */
#define BSON_ITER_HOLDS_BOOL(iter) /* ... */
#define BSON_ITER_HOLDS_DATE_TIME(iter) /* ... */
#define BSON_ITER_HOLDS_NULL(iter) /* ... */
#define BSON_ITER_HOLDS_REGEX(iter) /* ... */
#define BSON_ITER_HOLDS_DBPOINTER(iter) /* ... */
#define BSON_ITER_HOLDS_CODE(iter) /* ... */
#define BSON_ITER_HOLDS_SYMBOL(iter) /* ... */
#define BSON_ITER_HOLDS_CODEWSCOPE(iter) /* ... */
#define BSON_ITER_HOLDS_INT32(iter) /* ... */
#define BSON_ITER_HOLDS_TIMESTAMP(iter) /* ... */
#define BSON_ITER_HOLDS_INT64(iter) /* ... */
#define BSON_ITER_HOLDS_DECIMAL128(iter) /* ... */
#define BSON_ITER_HOLDS_MAXKEY(iter) /* ... */
#define BSON_ITER_HOLDS_MINKEY(iter) /* ... */
and then the combo macros, the "actually I'm flexible about this" ones π«Ά:
#define BSON_ITER_HOLDS_INT(iter) \
(BSON_ITER_HOLDS_INT32 (iter) || BSON_ITER_HOLDS_INT64 (iter))
#define BSON_ITER_HOLDS_NUMBER(iter) \
(BSON_ITER_HOLDS_INT (iter) || BSON_ITER_HOLDS_DOUBLE (iter))
#define BSON_ITER_IS_KEY(iter, key) \
(0 == strcmp ((key), bson_iter_key ((iter))))
and the struct itself β look at it, look away, it's shy:
typedef struct {
/*< private >*/
} bson_iter_t;
πΈ DESCRIPTION πΈ
so bson_iter_t is a struct you use to iterate through the elements of a bson_t. that's the whole vibe. π
it's meant to live on the stack, and you can toss it away whenever you want β like, whenever whenever β because it holds no external allocation. no cleanup era, no free() arc, nothing. β¨
the contents of the struct? consider them private. they may change between releases (ngl, they probably will), but the structure size will not change β that part's locked in, periodt.
β οΈ two rules, bestie, and they're not optional: - thebson_tMUST stay valid for the entire lifetime of theiter- modifying thebson_twhile you're using theiteris an error π«
π¦ EXAMPLES π¦
walk the whole doc and print every field name β the classic:
bson_iter_t iter;
if (bson_iter_init (&iter, my_bson_doc)) {
while (bson_iter_next (&iter)) {
printf ("Found a field named: %s\n", bson_iter_key (&iter));
}
}
only want one specific field? bson_iter_find has you covered:
bson_iter_t iter;
if (bson_iter_init (&iter, my_bson_doc) && bson_iter_find (&iter, "my_field")) {
printf ("Found the field named: %s\n", bson_iter_key (&iter));
}
need to go inside a sub-document or array? recurse into it like the main character you are π:
bson_iter_t iter;
bson_iter_t sub_iter;
if (bson_iter_init_find (&iter, my_bson_doc, "mysubdoc") &&
(BSON_ITER_HOLDS_DOCUMENT (&iter) || BSON_ITER_HOLDS_ARRAY (&iter)) &&
bson_iter_recurse (&iter, &sub_iter)) {
while (bson_iter_next (&sub_iter)) {
printf ("Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
}
}
and for the dotted-path girlies, bson_iter_find_descendant digging all the way down to a.b.c.d:
bson_iter_t iter;
if (bson_iter_init (&iter, my_doc) &&
bson_iter_find_descendant (&iter, "a.b.c.d", &sub_iter)) {
printf ("The type of a.b.c.d is: %d\n", (int) bson_iter_type (&sub_iter));
}
π AUTHOR π
MongoDB, Inc β the icons who wrote it π«Ά
π COPYRIGHT π
2009-present, MongoDB, Inc. β slay, that's a long reign.
okay that's the whole page, bestie!! this was libbson version 2.5.6, dated Oct 09, 2026, and honestly it ate. go iterate on something gorgeous π
β¨π
π¦ 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