<?xml version="1.0"?>
<page xmlns="http://projectmallard.org/1.0/"
      type="topic"
      style="function"
      xmlns:api="http://projectmallard.org/experimental/api/"
      xmlns:ui="http://projectmallard.org/experimental/ui/"
      id="bson_iter_init">
  <info>
    <link type="guide" xref="bson_iter_t" group="function"/>
  </info>
  <title>bson_iter_init()</title>

  <section id="synopsis">
    <title>Synopsis</title>
    <synopsis><code mime="text/x-csrc"><![CDATA[bool
bson_iter_init (bson_iter_t  *iter,
                const bson_t *bson);
]]></code></synopsis>
  </section>

  <section id="parameters">
    <title>Parameters</title>
    <table>
      <tr><td><p>iter</p></td><td><p>A <link xref="bson_iter_t">bson_iter_t</link>.</p></td></tr>
      <tr><td><p>bson</p></td><td><p>A <link xref="bson_t">bson_t</link>.</p></td></tr>
    </table>
  </section>

  <section id="description">
    <title>Description</title>
    <p>The <code>bson_iter_init()</code> function shall initialize <code>iter</code> to iterate upon the BSON document <code>bson</code>. Upon initialization, <code>iter</code> is placed before the first element. Callers must call <link xref="bson_iter_next">bson_iter_next()</link>, <link xref="bson_iter_find">bson_iter_find()</link>, or <link xref="bson_iter_find_case">bson_iter_find_case()</link> to advance to an element.</p>
  </section>

  <section id="return">
    <title>Returns</title>
    <p>Returns true if the iter was successfuly initialized.</p>
  </section>

  <section id="example">
    <title>Example</title>
    <synopsis><code mime="text/x-csrc"><![CDATA[
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");
   }
}

]]></code></synopsis>
  </section>
</page>
