int test_builder_api( mongo *conn ) { int count = 0; mongo_cursor cursor[1]; remove_sample_data( conn ); insert_sample_data( conn, 10000 ); mongo_cursor_init( cursor, conn, "test.cursors" ); while( mongo_cursor_next( cursor ) == MONGO_OK ) { count++; } ASSERT( count == 10000 ); mongo_cursor_destroy( cursor ); mongo_cursor_init( cursor, conn, "test.cursors" ); mongo_cursor_set_limit( cursor, 10 ); count = 0; while( mongo_cursor_next( cursor ) == MONGO_OK ) { count++; } ASSERT( count == 10 ); mongo_cursor_destroy( cursor ); return 0; }
int mongodb_update_key_stat(bot_t * bot, char *db, char *key, int value) { bson b; bson_iterator i; mongo_cursor cursor; int ret = 0; if (!db || !key || value < 0) { return -1; } debug(bot, "mongodb_update_key_stat: Entered\n"); bson_init(&b); bson_append_string(&b, "key", key); bson_finish(&b); mongo_cursor_init(&cursor, &gi->mongo_conn, db); mongo_cursor_set_query(&cursor, &b); if (mongo_cursor_next(&cursor) != MONGO_OK) { mongodb_insert_key_stat(bot, db, key, 0); mongo_cursor_init(&cursor, &gi->mongo_conn, db); mongo_cursor_set_query(&cursor, &b); if (mongo_cursor_next(&cursor) != MONGO_OK) goto cleanup; } debug(bot, "mongodb_update_key_stat: Found!\n"); if (bson_find(&i, mongo_cursor_bson(&cursor), "value")) { bson c; ret = (int)bson_iterator_int(&i); ret++; bson_init(&c); bson_append_string(&c, "key", key); bson_append_int(&c, "value", ret); bson_finish(&c); debug(bot, "mongodb_update_key_stat: updating to %i\n", ret); mongo_update(&gi->mongo_conn, db, &b, &c, 0); bson_destroy(&c); bson_destroy(&b); mongo_cursor_destroy(&cursor); return ret; } debug(bot, "mongodb_update_key_stat: Key not found\n"); cleanup: bson_destroy(&b); mongo_cursor_destroy(&cursor); return -1; }
bson_bool_t mongo_find_one(mongo_connection* conn, const char* ns, bson* query, bson* fields, bson* out){ mongo_cursor* cursor = mongo_find(conn, ns, query, fields, 1, 0, 0); if (cursor && mongo_cursor_next(cursor)){ bson_copy(out, &cursor->current); mongo_cursor_destroy(cursor); return 1; }else{ mongo_cursor_destroy(cursor); return 0; } }
static int mongo_cursor_get_more( mongo_cursor *cursor ) { int res; if( cursor->limit > 0 && cursor->seen >= cursor->limit ) { cursor->err = MONGO_CURSOR_EXHAUSTED; return MONGO_ERROR; } else if( ! cursor->reply ) { cursor->err = MONGO_CURSOR_INVALID; return MONGO_ERROR; } else if( ! cursor->reply->fields.cursorID ) { cursor->err = MONGO_CURSOR_EXHAUSTED; return MONGO_ERROR; } else { char *data; int sl = strlen( cursor->ns )+1; int limit = 0; mongo_message *mm; if( cursor->limit > 0 ) limit = cursor->limit - cursor->seen; mm = mongo_message_create( 16 /*header*/ +4 /*ZERO*/ +sl +4 /*numToReturn*/ +8 /*cursorID*/ , 0, 0, MONGO_OP_GET_MORE ); data = &mm->data; data = mongo_data_append32( data, &ZERO ); data = mongo_data_append( data, cursor->ns, sl ); data = mongo_data_append32( data, &limit ); data = mongo_data_append64( data, &cursor->reply->fields.cursorID ); bson_free( cursor->reply ); res = mongo_message_send( cursor->conn, mm ); if( res != MONGO_OK ) { mongo_cursor_destroy( cursor ); return MONGO_ERROR; } res = mongo_read_response( cursor->conn, &( cursor->reply ) ); if( res != MONGO_OK ) { mongo_cursor_destroy( cursor ); return MONGO_ERROR; } cursor->current.data = NULL; cursor->seen += cursor->reply->fields.num; return MONGO_OK; } }
int mongodb_insert_key(bot_t * bot, char *db, char *key, char *value, char *fmt, ...) { bson b; mongo_cursor cursor; va_list ap; char buf[1024], *buf_ptr = "NULL"; if (!db || !key || !value) { return -1; } debug(bot, "mongodb_insert_key: Entered\n"); if (fmt) { bz(buf); va_start(ap, fmt); vsnprintf_buf(buf, fmt, ap); va_end(ap); buf_ptr = buf; } bson_init(&b); bson_append_string(&b, "key", key); bson_finish(&b); mongo_cursor_init(&cursor, &gi->mongo_conn, db); mongo_cursor_set_query(&cursor, &b); if (mongo_cursor_next(&cursor) == MONGO_OK) { debug(bot, "mongodb_insert_key: Key already exist\n"); bson_destroy(&b); mongo_cursor_destroy(&cursor); return -1; } bson_init(&b); bson_append_string(&b, "key", key); bson_append_string(&b, "value", value); bson_append_string(&b, "comment", buf_ptr); bson_finish(&b); mongo_insert(&gi->mongo_conn, db, &b); bson_destroy(&b); mongo_cursor_destroy(&cursor); return 0; }
int mongo_find_one( mongo *conn, const char *ns, bson *query, bson *fields, bson *out ) { mongo_cursor *cursor = mongo_find( conn, ns, query, fields, 1, 0, 0 ); if ( cursor && mongo_cursor_next( cursor ) == MONGO_OK ) { bson_copy_basic( out, &cursor->current ); mongo_cursor_destroy( cursor ); return MONGO_OK; } else { mongo_cursor_destroy( cursor ); return MONGO_ERROR; } }
bson_bool_t mongo_cursor_get_more(mongo_cursor* cursor){ if (cursor->mm && cursor->mm->fields.cursorID){ mongo_connection* conn = cursor->conn; char* data; int sl = strlen(cursor->ns)+1; mongo_message * mm = mongo_message_create(16 /*header*/ +4 /*ZERO*/ +sl +4 /*numToReturn*/ +8 /*cursorID*/ , 0, 0, mongo_op_get_more); data = &mm->data; data = mongo_data_append32(data, &zero); data = mongo_data_append(data, cursor->ns, sl); data = mongo_data_append32(data, &zero); data = mongo_data_append64(data, &cursor->mm->fields.cursorID); mongo_message_send(conn, mm); free(cursor->mm); MONGO_TRY{ cursor->mm = mongo_read_response(cursor->conn); }MONGO_CATCH{ cursor->mm = NULL; mongo_cursor_destroy(cursor); MONGO_RETHROW(); } return cursor->mm && cursor->mm->fields.num; } else{
int inode_exists(const char * path) { bson query, fields; mongo * conn = get_conn(); mongo_cursor curs; int res; bson_init(&query); bson_append_string(&query, "dirents", path); bson_finish(&query); bson_init(&fields); bson_append_int(&fields, "dirents", 1); bson_append_int(&fields, "_id", 0); bson_finish(&fields); mongo_cursor_init(&curs, conn, inodes_name); mongo_cursor_set_query(&curs, &query); mongo_cursor_set_fields(&curs, &fields); mongo_cursor_set_limit(&curs, 1); res = mongo_cursor_next(&curs); bson_destroy(&query); bson_destroy(&fields); mongo_cursor_destroy(&curs); if(res == 0) return 0; if(curs.err != MONGO_CURSOR_EXHAUSTED) return -EIO; return -ENOENT; }
static void close_cursor(MongoCursor::cursor* cur) { JSFiber::scope s; mongo_cursor_destroy(cur); delete cur; }
int users_search(mongo* conn, User* users, int limit) { int pos = 0; mongo_cursor cursor[1]; bson_iterator iterator[1]; mongo_cursor_init(cursor, conn, "ruby-mongo-console.users"); // mongo_cursor_set_query mongo_cursor_set_limit(cursor, limit); while(mongo_cursor_next(cursor) == MONGO_OK) { const char* s; s = get_field_value(iterator, cursor, "name"); if (s) { users[pos].name_len = strlen(s); users[pos].name = new_str(s, users[pos].name_len); } else { users[pos].name_len = 0; users[pos].name = NULL; } s = get_field_value(iterator, cursor, "bio"); if (s) { users[pos].bio_len = strlen(s); users[pos].bio = new_str(s, users[pos].bio_len); } else { users[pos].bio_len = 0; users[pos].bio = NULL; } pos++; } mongo_cursor_destroy(cursor); return pos; }
static void find_range( const char *ns ) { int i; bson bb; mongo_cursor *cursor; for ( i=0; i < PER_TRIAL; i++ ) { int j=0; bson_init( &bb ); bson_append_start_object( &bb, "x" ); bson_append_int( &bb, "$gt", PER_TRIAL/2 ); bson_append_int( &bb, "$lt", PER_TRIAL/2 + BATCH_SIZE ); bson_append_finish_object( &bb ); bson_finish( &bb ); cursor = mongo_find( conn, ns, &bb, NULL, 0,0,0 ); ASSERT( cursor ); while( mongo_cursor_next( cursor ) == MONGO_OK ) { j++; } ASSERT( j == BATCH_SIZE-1 ); mongo_cursor_destroy( cursor ); bson_destroy( &bb ); } }
/* * __gc */ static int cursor_gc(lua_State *L) { MongoCursor *cursor = userdata_to_cursor(L, 1); mongo_cursor_destroy(&cursor->cursor); bson_destroy(&cursor->fields); bson_destroy(&cursor->query); return 0; }
static void tutorial_empty_query( mongo *conn) { mongo_cursor cursor[1]; mongo_cursor_init( cursor, conn, "tutorial.persons" ); while( mongo_cursor_next( cursor ) == MONGO_OK ) bson_print( &cursor->current ); mongo_cursor_destroy( cursor ); }
static void ngx_http_gridfs_cleanup(void* data) { ngx_http_gridfs_cleanup_t* gridfs_clndata; volatile ngx_uint_t i; gridfs_clndata = data; for (i = 0; i < gridfs_clndata->numchunks; i++) { mongo_cursor_destroy(gridfs_clndata->cursors[i]); } }
SEXP rmongo_cursor_destroy(SEXP cursor) { mongo_cursor* _cursor = _checkCursor(cursor); SEXP ret; PROTECT(ret = allocVector(LGLSXP, 1)); LOGICAL(ret)[0] = mongo_cursor_destroy(_cursor); SEXP ptr = getAttrib(cursor, sym_mongo_cursor); R_ClearExternalPtr(ptr); UNPROTECT(1); return ret; }
char *mongodb_retrieve_key(bot_t * bot, char *db, char *key, char *which) { char *str = NULL; bson b; mongo_cursor cursor; if (!db || !key || !which) { return NULL; } debug(bot, "mongodb_retrieve_key: Entered :db=%s. key=%s, which=%s\n", db, key, which); bson_init(&b); bson_append_string(&b, "key", key); bson_finish(&b); mongo_cursor_init(&cursor, &gi->mongo_conn, db); mongo_cursor_set_query(&cursor, &b); if (mongo_cursor_next(&cursor) == MONGO_OK) { bson_iterator i; debug(bot, "mongodb_retrieve_key: Found!\n"); if (bson_find(&i, mongo_cursor_bson(&cursor), which)) { str = (char *)bson_iterator_string(&i); str = strdup(str); } bson_destroy(&b); mongo_cursor_destroy(&cursor); return str; } debug(bot, "mongodb_retrieve_key: Key not found\n"); bson_destroy(&b); mongo_cursor_destroy(&cursor); return NULL; }
MONGO_EXPORT int mongo_find_one( mongo *conn, const char *ns, const bson *query, const bson *fields, bson *out ) { mongo_cursor cursor[1]; mongo_cursor_init( cursor, conn, ns ); mongo_cursor_set_query( cursor, query ); mongo_cursor_set_fields( cursor, fields ); mongo_cursor_set_limit( cursor, 1 ); if ( mongo_cursor_next( cursor ) == MONGO_OK ) { bson_init_size( out, bson_size( (bson *)&cursor->current ) ); memcpy( out->data, cursor->current.data, bson_size( (bson *)&cursor->current ) ); out->finished = 1; mongo_cursor_destroy( cursor ); return MONGO_OK; } else { mongo_cursor_destroy( cursor ); return MONGO_ERROR; } }
gridfs_offset gridfile_read(gridfile* gfile, gridfs_offset size, char* buf) { mongo_cursor* chunks; bson chunk; int first_chunk; int last_chunk; int total_chunks; gridfs_offset chunksize; gridfs_offset contentlength; gridfs_offset bytes_left; int i; bson_iterator it; gridfs_offset chunk_len; const char * chunk_data; contentlength = gridfile_get_contentlength(gfile); chunksize = gridfile_get_chunksize(gfile); size = (contentlength - gfile->pos < size) ? contentlength - gfile->pos : size; bytes_left = size; first_chunk = (gfile->pos)/chunksize; last_chunk = (gfile->pos+size-1)/chunksize; total_chunks = last_chunk - first_chunk + 1; chunks = gridfile_get_chunks(gfile, first_chunk, total_chunks); for (i = 0; i < total_chunks; i++) { mongo_cursor_next(chunks); chunk = chunks->current; bson_find(&it, &chunk, "data"); chunk_len = bson_iterator_bin_len( &it ); chunk_data = bson_iterator_bin_data( &it ); if (i == 0) { chunk_data += (gfile->pos)%chunksize; chunk_len -= (gfile->pos)%chunksize; } if (bytes_left > chunk_len) { memcpy(buf, chunk_data, chunk_len); bytes_left -= chunk_len; buf += chunk_len; } else { memcpy(buf, chunk_data, bytes_left); } } mongo_cursor_destroy(chunks); gfile->pos = gfile->pos + size; return size; }
int mongodb_retrieve_key_stat(bot_t * bot, char *db, char *key) { bson b; mongo_cursor cursor; int ret = 0; if (!db || !key) { return -1; } debug(bot, "mongodb_retrieve_key: Entered\n"); bson_init(&b); bson_append_string(&b, "key", key); bson_finish(&b); mongo_cursor_init(&cursor, &gi->mongo_conn, db); mongo_cursor_set_query(&cursor, &b); if (mongo_cursor_next(&cursor) == MONGO_OK) { bson_iterator i; debug(bot, "mongodb_retrieve_key: Found!\n"); if (bson_find(&i, mongo_cursor_bson(&cursor), "value")) { ret = (int)bson_iterator_int(&i); } bson_destroy(&b); mongo_cursor_destroy(&cursor); return ret; } debug(bot, "mongodb_retrieve_key: Key not found\n"); bson_destroy(&b); mongo_cursor_destroy(&cursor); return -1; }
SEXP mongo_get_database_collections(SEXP mongo_conn, SEXP db) { mongo* conn = _checkMongo(mongo_conn); const char* _db = CHAR(STRING_ELT(db, 0)); int len = strlen(_db); char ns[512]; strcpy(ns, _db); strcpy(ns+len, ".system.namespaces"); bson empty; bson_empty(&empty); mongo_cursor* cursor = mongo_find(conn, ns, NULL, &empty, 0, 0, 0); int count = 0; while (cursor && mongo_cursor_next(cursor) == MONGO_OK) { bson_iterator iter; if (bson_find(&iter, &cursor->current, "name")) { const char* name = bson_iterator_string(&iter); if (strstr(name, ".system.") || strchr(name, '$')) continue; ++count; } } mongo_cursor_destroy(cursor); cursor = mongo_find(conn, ns, &empty, &empty, 0, 0, 0); SEXP ret; PROTECT(ret = allocVector(STRSXP, count)); int i = 0; while (cursor && mongo_cursor_next(cursor) == MONGO_OK) { bson_iterator iter; if (bson_find(&iter, &cursor->current, "name")) { const char* name = bson_iterator_string(&iter); if (strstr(name, ".system.") || strchr(name, '$')) continue; SET_STRING_ELT(ret, i++, mkChar(name)); } } mongo_cursor_destroy(cursor); UNPROTECT(1); return ret; }
dlist_t *mongodb_dump_keyvalue(bot_t * bot, char *db) { dlist_t *dl, *dptr; keyvalue_t *keyvalue_ptr; mongo_cursor *cursor; bson b; bson_iterator bi; char *key; char *value; dl = dptr = NULL; cursor = NULL; if (!db) return NULL; debug(bot, "mongodb_dump_keyvalue: Entered\n"); cursor = mongo_find(&gi->mongo_conn, db, bson_empty(&b), bson_empty(&b), 0, 0, 0); while (mongo_cursor_next(cursor) == MONGO_OK) { key = value = NULL; if (bson_find(&bi, mongo_cursor_bson(cursor), "key")) { key = (char *)bson_iterator_string(&bi); } if (bson_find(&bi, mongo_cursor_bson(cursor), "value")) { value = (char *)bson_iterator_string(&bi); } if (!key && !value) continue; keyvalue_ptr = (keyvalue_t *) calloc(1, sizeof(keyvalue_t)); if (key) keyvalue_ptr->key = strdup(key); if (value) keyvalue_ptr->value = strdup(value); dlist_Dinsert_after(&dl, keyvalue_ptr); } mongo_cursor_destroy(cursor); return dl; }
static ngx_int_t ngx_http_mongo_authenticate(ngx_log_t *log, ngx_http_gridfs_loc_conf_t *gridfs_loc_conf) { ngx_http_mongo_connection_t* mongo_conn; ngx_http_mongo_auth_t *mongo_auth; mongo_cursor *cursor = NULL; bson empty; char *test; int error; mongo_conn = ngx_http_get_mongo_connection( gridfs_loc_conf->mongo ); if (mongo_conn == NULL) { ngx_log_error(NGX_LOG_ERR, log, 0, "Mongo Connection not found: \"%V\"", &gridfs_loc_conf->mongo); } // Authenticate if (gridfs_loc_conf->user.data != NULL && gridfs_loc_conf->pass.data != NULL) { if (mongo_cmd_authenticate( &mongo_conn->conn, (const char*)gridfs_loc_conf->db.data, (const char*)gridfs_loc_conf->user.data, (const char*)gridfs_loc_conf->pass.data ) != MONGO_OK) { ngx_log_error(NGX_LOG_ERR, log, 0, "Invalid mongo user/pass: %s/%s", gridfs_loc_conf->user.data, gridfs_loc_conf->pass.data); return NGX_ERROR; } mongo_auth = ngx_array_push(mongo_conn->auths); mongo_auth->db = gridfs_loc_conf->db; mongo_auth->user = gridfs_loc_conf->user; mongo_auth->pass = gridfs_loc_conf->pass; } // Run a test command to test authentication. test = (char*)malloc( gridfs_loc_conf->db.len + sizeof(".test")); ngx_cpystrn((u_char*)test, (u_char*)gridfs_loc_conf->db.data, gridfs_loc_conf->db.len+1); ngx_cpystrn((u_char*)(test+gridfs_loc_conf->db.len),(u_char*)".test", sizeof(".test")); bson_empty(&empty); cursor = mongo_find(&mongo_conn->conn, test, &empty, NULL, 0, 0, 0); error = mongo_cmd_get_last_error(&mongo_conn->conn, (char*)gridfs_loc_conf->db.data, NULL); free(test); mongo_cursor_destroy(cursor); if (error) { ngx_log_error(NGX_LOG_ERR, log, 0, "Authentication Required"); return NGX_ERROR; } return NGX_OK; }
MongoCursor::~MongoCursor() { m_query.Reset(); if (in_gc()) syncCall(close_cursor, m_cursor); else { mongo_cursor_destroy(m_cursor); delete m_cursor; } if (m_bInit) bson_destroy(&m_bbq); bson_destroy(&m_bbp); }
static void find( const char *ns ) { bson b; int i; for ( i=0; i < PER_TRIAL; i++ ) { mongo_cursor *cursor; make_query( &b ); cursor = mongo_find( conn, ns, &b, NULL, 0,0,0 ); ASSERT( cursor ); while( mongo_cursor_next( cursor ) == MONGO_OK ) {} mongo_cursor_destroy( cursor ); bson_destroy( &b ); } }
/* *-------------------------------------------------------------- * * mongotcl_cursorObjectDelete -- command deletion callback routine. * * Results: * ...frees the mongo cursor object. * ...frees memory * * Side effects: * None. * *-------------------------------------------------------------- */ void mongotcl_cursorObjectDelete (ClientData clientData) { mongotcl_cursorClientData *mc = (mongotcl_cursorClientData *)clientData; assert (mc->cursor_magic == MONGOTCL_CURSOR_MAGIC); mongo_cursor_destroy(mc->cursor); if (mc->fieldsBson != NULL) { bson_destroy (mc->fieldsBson); ckfree ((char *)mc->fieldsBson); } ckfree((char *)mc->cursor); ckfree((char *)clientData); }
int test_tailable( mongo *conn ) { mongo_cursor *cursor; bson b; int count; remove_sample_data( conn ); create_capped_collection( conn ); insert_sample_data( conn, 10000 ); bson_init( &b ); bson_append_start_object( &b, "$query" ); bson_append_finish_object( &b ); bson_append_start_object( &b, "$sort" ); bson_append_int( &b, "$natural", -1 ); bson_append_finish_object( &b ); bson_finish( &b ); cursor = mongo_find( conn, "test.cursors", &b, bson_shared_empty( ), 0, 0, MONGO_TAILABLE ); bson_destroy( &b ); count = 0; while( mongo_cursor_next( cursor ) == MONGO_OK ) count++; ASSERT( count == 10000 ); ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR ); ASSERT( cursor->err == MONGO_CURSOR_PENDING ); insert_sample_data( conn, 10 ); count = 0; while( mongo_cursor_next( cursor ) == MONGO_OK ) { count++; } ASSERT( count == 10 ); ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR ); ASSERT( cursor->err == MONGO_CURSOR_PENDING ); mongo_cursor_destroy( cursor ); remove_sample_data( conn ); return 0; }
UInfo* MongoLink::query(int uid) { UInfo* uinfo = NULL; bson query[1]; mongo_cursor cursor[1]; bson_init( query ); bson_append_int( query, "uid", uid ); bson_finish( query ); mongo_cursor_init( cursor, &m_mongo, m_strDBName.c_str() ); mongo_cursor_set_query( cursor, query ); if( mongo_cursor_next( cursor ) == MONGO_OK ) { bson_iterator iterator[1]; uinfo = new UInfo(); //uinfo->linkid = 0; if ( bson_find( iterator, mongo_cursor_bson( cursor ), "uid" ) != BSON_EOO ) { uinfo->uid= bson_iterator_int( iterator ); } if ( bson_find( iterator, mongo_cursor_bson( cursor ), "router" ) != BSON_EOO ) { uinfo->router= bson_iterator_string( iterator ); } if ( bson_find( iterator, mongo_cursor_bson( cursor ), "dispatcher" ) != BSON_EOO ) { uinfo->dispatcher= bson_iterator_string( iterator ); } if ( bson_find( iterator, mongo_cursor_bson( cursor ), "proxy" ) != BSON_EOO ) { uinfo->proxy= bson_iterator_string( iterator ); } } else { LOG(TAG_ROUTER, "query uid failed, uid=%d.", uid); } exit: bson_destroy( query ); mongo_cursor_destroy( cursor ); return uinfo; }
int test_copy_cursor_data( mongo *conn ) { mongo_cursor cursor[1]; bson b[1]; insert_sample_data( conn, 10 ); mongo_cursor_init( cursor, conn, "test.cursors" ); mongo_cursor_next( cursor ); ASSERT( bson_copy( b, mongo_cursor_bson( cursor ) ) == MONGO_OK ); ASSERT( memcmp( (void *)b->data, (void *)(cursor->current).data, bson_size( &cursor->current ) ) == 0 ); mongo_cursor_destroy( cursor ); bson_destroy( b ); return 0; }
MONGO_EXPORT mongo_cursor *mongo_find( mongo *conn, const char *ns, const bson *query, const bson *fields, int limit, int skip, int options ) { mongo_cursor *cursor = ( mongo_cursor * )bson_malloc( sizeof( mongo_cursor ) ); mongo_cursor_init( cursor, conn, ns ); cursor->flags |= MONGO_CURSOR_MUST_FREE; mongo_cursor_set_query( cursor, query ); mongo_cursor_set_fields( cursor, fields ); mongo_cursor_set_limit( cursor, limit ); mongo_cursor_set_skip( cursor, skip ); mongo_cursor_set_options( cursor, options ); if( mongo_cursor_op_query( cursor ) == MONGO_OK ) return cursor; else { mongo_cursor_destroy( cursor ); return NULL; } }
static void tutorial_simple_query( mongo *conn ) { bson query[1]; mongo_cursor cursor[1]; bson_init( query ); bson_append_int( query, "age", 24 ); bson_finish( query ); mongo_cursor_init( cursor, conn, "tutorial.persons" ); mongo_cursor_set_query( cursor, query ); while( mongo_cursor_next( cursor ) == MONGO_OK ) { bson_iterator iterator[1]; if ( bson_find( iterator, mongo_cursor_bson( cursor ), "name" )) { printf( "name: %s\n", bson_iterator_string( iterator ) ); } } bson_destroy( query ); mongo_cursor_destroy( cursor ); }