0

Here's the structure part of my collection :

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

Which method could you advise me to retrieve the list of values in the "likes" field with C lib please ?

John S
  • 231
  • 3
  • 11

1 Answers1

2

I don't have a working MongoDB C driver, but this should help get you started. Also, the docs should help you (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 );
        }
    }
}
WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • I have edited my post with a new question ! Can you help me ? – John S Sep 09 '13 at 23:12
  • @JohnS-that's not really how StackOverflow works. :( Please try it on your own and post where you've gotten stuck in a new question. I'd imagine you'd use `bson_find` on the `sub` for `"name"`. – WiredPrairie Sep 10 '13 at 00:14