如何在MongoDB-C驱动程序中找到特定值

时间:2014-03-08 12:47:05

标签: mongodb bson mongodb-c

当我的结构看起来像这样:

X: {
   Y: "blabla"
}

所以我使用函数“bson_iter_find_descendant(& iter,” X.Y “,& desc)”来恢复我的数据。但是当我的结构如下所示时,如何检索“ X.Y ”,“ X.Z ”...的值:

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

请注意,我使用最新版本的MongoDB-C驱动程序...

提前谢谢!

1 个答案:

答案 0 :(得分:2)

// 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

然后

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));