---
name: "bson_iter_init"
section: "3"
platform: "linux"
package: "libbson-doc"
version: "1.30.4-1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/bson_iter_init"
---

# bson\_iter\_init(3)

## SYNOPSIS

```
bool
bson_iter_init (bson_iter_t *iter, const bson_t *bson);
```

## PARAMETERS

- •

  **iter**: A *bson\_iter\_t*.

- •

  **bson**: A *bson\_t*.

## DESCRIPTION

The **bson\_iter\_init()** function shall initialize **iter** to iterate upon the BSON document **bson**. Upon initialization, **iter** is placed before the first element. Callers must call *bson\_iter\_next()*, *bson\_iter\_find()*, or *bson\_iter\_find\_case()* to advance to an element.

## RETURNS

Returns true if the iter was successfully initialized.

## EXAMPLE

```
static void
print_doc_id (const bson_t *doc)
{
   bson_iter_t iter;
   bson_oid_t oid;
   char oidstr[25];
   if (bson_iter_init (&iter, doc) && 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");
   }
}
/* alternatively */
static void
print_doc_id (const bson_t *doc)
{
   bson_iter_t iter;
   bson_oid_t oid;
   char oidstr[25];
   if (bson_iter_init_find (&iter, doc, "_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");
   }
}
```

## AUTHOR

MongoDB, Inc

## COPYRIGHT

2009-present, MongoDB, Inc.
