mongodbbsonmongodb-c

How to find specific value in MongoDB-C driver


When my structure looks like this:

X: {
   Y: "blabla"
}

So I use the function "bson_iter_find_descendant(&iter, "X.Y", &desc)" to recover my data. But how to retrieve the values of "X.Y", "X.Z"... when my structure looks like this:

X: [
   {
      Y: "blihblih"
   },
   {
      Z: "bloublou"
   }
]

Note that I use the latest version of MongoDB-C Driver ...

Thank you in advance!


Solution

  • //MONGOC_VERSION_S "0.92.3" ./configure --with-libbson=bundled

    const bson_t *doc;
    bson_iter_t iter;
    bson_iter_t iter2;
    uint32_t length;
    

    mongoc_client_get_collection mongoc_collection_find mongoc_cursor_next

    and then

    bson_iter_init(&iter,doc);
    bson_iter_find_descendant(&iter,"X.0.Y",&iter2); // for first
    bson_iter_find_descendant(&iter,"X.1.Z",&iter2); // for second array element
    
    printf("%d\n",bson_iter_type(&iter2));
    
    for type 2
    printf("%s\n",bson_iter_utf8(&iter2,&length));