I meet a problem on C program connect to mongodb to retrieve document...I dont know how to solve it, please help...
The record structure :
{ "District" : "HK",
"Contact": [ {"name":"Person A","telephone":"1111-1111"} ,
{"name":"Person B", "telephone":"2222-2222} ]
}
Here is my code:
while( mongo_cursor_next( cursor ) == MONGO_OK ){
bson_iterator iterator[1];
//print district
if ( bson_find( iterator, mongo_cursor_bson( cursor ), "District" )) {
printf( "District: %s\n", bson_iterator_string( iterator ) );
//print array elements
if ( bson_find( iterator, mongo_cursor_bson( cursor ), "Contact" )) {
bson_iterator subit[1];
bson_iterator_subiterator(iterator, subit);
//get array list element one by one
while(bson_iterator_more(subit)){
if(bson_iterator_next(subit)!=BSON_EOO){
bson sub_Object[1];
bson_iterator_subobject_init(subit, sub_Object,1);
//bson_print(sub_Object);
//comment out the following bson_find could show the expected result
if(bson_find(subit, sub_Object, "name"))
printf("\tName : %s\n", bson_iterator_string(subit));
if(bson_find(subit, sub_Object, "telephone"))
printf("\tTelephone: %s\n", bson_iterator_string(subit));
bson_destroy(sub_Object);
}
}
}
}
Output
District: HK
Name: Person A
Telephone: 1111-1111
Any one know why Person B record disappear??
I have test if DO NOT use bson_find inside the second while loop, it could able to print out all the element by bson_print!!
Is there a bug on mongodb ?? Or my code is wrong?
Thank you very much!!!