我正在尝试使用 c 驱动程序从 MongoDB 文档中提取数组。该文档的结构如下:
name: "TestName"
data:["testData1","testData2"];
我检索了这样的文档:
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &document))
{
str = (const unsigned char *)bson_as_canonical_extended_json(document, NULL);
b = bson_new_from_json(str, -1, &error);
}
if (bson_iter_init(&iter, b))
{
while (bson_iter_next(&iter))
{
//if the type is an array
if (bson_iter_type(&iter) == 4){
//requesting for data
bson_iter_find(&iter, "data");
const uint8_t * data = NULL;
uint32_t len = 0;
bson_iter_array(&iter, &len, &data);
bson_t *dataArray = bson_new_from_data(data, len);
bson_iter_t dataIter;
bson_iter_init(&dataIter, dataArray);
bson_iter_find(&dataIter, "0");
}
}
}
调试器在 bson_iter_array() 处显示失败。我已经引用了这篇文章,但无法解决这个问题。如何使用 c 驱动程序从 MongoDB 的数组中正确检索和存储数据?