mongodb C驱动程序查询以选择特定字段
我读到MongoDB中只能选择一个字段:mongodb C驱动程序查询以选择特定字段,c,mongodb,field,C,Mongodb,Field,我读到MongoDB中只能选择一个字段: db.records.find({TimeStampSec {$lt:10} , {ch11:1}) 例如,此查询返回所有具有TimeStampSec的对象的字段ch11。我不完全熟悉c api,但我想您应该添加第二个json obj 所以最终是这样的: bson_init( &query ); bson_append_start_object( &query, "TimeStampSec" ); bson_append_i
db.records.find({TimeStampSec {$lt:10} , {ch11:1})
例如,此查询返回所有具有TimeStampSec的对象的字段ch11。我不完全熟悉c api,但我想您应该添加第二个json obj 所以最终是这样的:
bson_init( &query );
bson_append_start_object( &query, "TimeStampSec" );
bson_append_int( &query, "$lt", 10 );
bson_append_finish_object( &query );
bson_append_start_object( &query, "$query" );
bson_append_int( &query, "ch11", 1 );
bson_append_finish_object( &query );
bson_finish( &query );
这对我很有用
bson query,selection;
bson_init( &query );
bson_append_start_object( &query, "TimeStampSec" );
bson_append_int( &query, "$lt", 10 );
bson_append_finish_object( &query );
bson_finish( &query );
bson_init( &selection);
bson_append_int( &selection, "ch11", 1 );
bson_finish( &selection);
mongo_cursor *cursor=NULL;
cursor = mongo_find( conn, "db.records",query ,selection,0,0,0);
if(cursor!=NULL)
{
while( mongo_cursor_next( cursor ) == MONGO_OK )
bson_print( &cursor->current );
}
bson query,selection;
bson_init( &query );
bson_append_start_object( &query, "TimeStampSec" );
bson_append_int( &query, "$lt", 10 );
bson_append_finish_object( &query );
bson_finish( &query );
bson_init( &selection);
bson_append_int( &selection, "ch11", 1 );
bson_finish( &selection);
mongo_cursor *cursor=NULL;
cursor = mongo_find( conn, "db.records",query ,selection,0,0,0);
if(cursor!=NULL)
{
while( mongo_cursor_next( cursor ) == MONGO_OK )
bson_print( &cursor->current );
}