1

我使用 C 驱动程序对 mongoDB 进行操作,当我执行此处显示的简单“查找”操作时:

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

我想知道是否有任何方法可以在不转换为 json 字符串的情况下获取 bson 对象,如此处显示的 c++ 驱动程序所示:
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];
4

0 回答 0