1BSON_ITER_INIT(3) libbson BSON_ITER_INIT(3)
2
3
4
6 bool
7 bson_iter_init (bson_iter_t *iter, const bson_t *bson);
8
10 • iter: A bson_iter_t.
11
12 • bson: A bson_t.
13
15 The bson_iter_init() function shall initialize iter to iterate upon the
16 BSON document bson. Upon initialization, iter is placed before the
17 first element. Callers must call bson_iter_next(), bson_iter_find(), or
18 bson_iter_find_case() to advance to an element.
19
21 Returns true if the iter was successfully initialized.
22
24 static void
25 print_doc_id (const bson_t *doc)
26 {
27 bson_iter_t iter;
28 bson_oid_t oid;
29 char oidstr[25];
30
31 if (bson_iter_init (&iter, doc) && bson_iter_find (&iter, "_id") &&
32 BSON_ITER_HOLDS_OID (&iter)) {
33 bson_iter_oid (&iter, &oid);
34 bson_oid_to_string (&oid, oidstr);
35 printf ("%s\n", oidstr);
36 } else {
37 printf ("Document is missing _id.\n");
38 }
39 }
40
41 /* alternatively */
42
43 static void
44 print_doc_id (const bson_t *doc)
45 {
46 bson_iter_t iter;
47 bson_oid_t oid;
48 char oidstr[25];
49
50 if (bson_iter_init_find (&iter, doc, "_id") && BSON_ITER_HOLDS_OID (&iter)) {
51 bson_iter_oid (&iter, &oid);
52 bson_oid_to_string (&oid, oidstr);
53 printf ("%s\n", oidstr);
54 } else {
55 printf ("Document is missing _id.\n");
56 }
57 }
58
60 MongoDB, Inc
61
63 2017-present, MongoDB, Inc
64
65
66
67
681.25.1 Nov 08, 2023 BSON_ITER_INIT(3)