NAME

bson_iter_init() - 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.

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(3) 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(3) , bson_iter_find(3) , or bson_iter_find_case(3) to advance to an element.

RETURNS

Returns true if the iter was successfuly 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");
   }
}

COLOPHON

This page is part of libbson. Please report any bugs at https://jira.mongodb.org/browse/CDRIVER.