在mongodb C驱动程序中使用列表

时间:2013-09-09 20:28:59

标签: mongodb bson mongodb-c

这是我的收藏的结构部分:

{
   ...
   likes: ['6a6ca923517f304900badd98','6a6ca923517f304900badd99','...'],
   ...
}

您可以建议我使用C lib检索“赞”字段中的值列表吗?

1 个答案:

答案 0 :(得分:2)

我没有工作的MongoDB C驱动程序,但这应该有助于您入门。此外,文档应该帮助您(here)。

bson_iterator i[1], sub[i];
bson_type type;
const char * key;
const char * value;

// do query, get cursor

while(mongo_cursor_next(cursor) == MONGO_OK) {
    // look for the "likes" field
    if( bson_find( iterator, bson, "likes" )) {
        // need to iterate through the elements of the array
        bson_iterator_subiterator( iterator, sub );

        // then iterate using "sub", until returns a BSON_EOO
        while (BSON_EOO != bson_iterator_next( sub )) {
            key = bson_iterator_key( sub );
            // if it's a string...
            value = bson_iterator_string( sub );
        }
    }
}