title: how to extract the raw value in a subdocument in mongo's libbson c library?
body: <p>consider the mongo document:  <code>{&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: {&quot;subkey1&quot;: &quot;subvalue1&quot;}}</code>, this can be encoded (using python bson module like: bson.encode(DATA)) to the following uint8_t array: <code>[56, 0, 0, 0, 2, 107, 101, 121, 49, 0, 7, 0, 0, 0, 118, 97, 108, 117, 101, 49, 0, 3, 107, 101, 121, 50, 0, 28, 0, 0, 0, 2, 115, 117, 98, 107, 101, 121, 49, 0, 10, 0, 0, 0, 115, 117, 98, 118, 97, 108, 117, 101, 49, 0, 0, 0]</code>. Now, I use this array to initialize the bson_t struct and use the iterator to find the subdocument for <code>key2</code>.</p>
  <p>Here I want to create another bson_t document from this. I tried using the bson_iter_document() method but it gives me <code>precondition failed: document</code> error. Maybe i'm not using it right. Is there another way to do it properly?</p>
  <pre><code>void test_bson(){
      uint8_t raw[] = {56, 0, 0, 0, 2, 107, 101, 121, 49, 0, 7, 0, 0, 0, 118, 97, 108, 117, 101, 49, 0, 3, 107, 101, 121, 50, 0, 28, 0, 0, 0, 2, 115, 117, 98, 107, 101, 121, 49, 0, 10, 0, 0, 0, 115, 117, 98, 118, 97, 108, 117, 101, 49, 0, 0, 0};
      bson_t *bson;
      bson = bson_new_from_data(raw, 56);
  
      bson_iter_t iter;
      bson_iter_init(&amp;iter, bson);
  
      if (bson_iter_find(&amp;iter, &quot;key2&quot;)){
          printf(&quot;found. %d\n&quot;, bson_iter_type(&amp;iter));
          const uint8_t **subdocument;
          uint32_t subdoclen = 56;
          bson_iter_document (&amp;iter, &amp;subdoclen, subdocument);
      }
  
      bson_free(bson);
  }
  </code></pre>