1

Im using the C driver to make operations on mongoDB , when i do simple "find" operation shown here :

while (mongoc_cursor_next (cursor, &doc)) {
     str = bson_as_json (doc, NULL);
     printf ("%s\n", str);
     bson_free (str);
}

i want to know if there is any way to get the bson objects without converting to json string as shown in the c++ driver shown here :
https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/bsoncxx/getting_values.cpp

auto doc = build_doc.view();


// Once we have the document view, we can use ["key"] or [index] notation to reach into nested
// documents or arrays.
auto awards = doc["awards"];
auto first_award_year = awards[0]["year"];
auto second_award_year = doc["awards"][1]["year"];
auto last_name = doc["name"]["last"];


// If the key doesn't exist, or index is out of bounds, we get invalid elements.
auto invalid1 = doc["name"]["middle"];
auto invalid2 = doc["contribs"][1000];
user63898
  • 29,839
  • 85
  • 272
  • 514

0 Answers0