0

これが私のコレクションの構造部分です:

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

C libで「likes」フィールドの値のリストを取得するには、どの方法をアドバイスしてもらえますか?

4

1 に答える 1

2

私は動作する MongoDB C ドライバーを持っていませんが、これは始めるのに役立つはずです。また、ドキュメントが役立つはずです (ここ)。

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 );
        }
    }
}
于 2013-09-09T21:18:39.890 に答える