我读过可以在MongoDB中只选择一个字段:
db.records.find({TimeStampSec {$lt:10} , {ch11:1})
例如,此查询返回具有TimeStampSec< 10的所有对象的字段ch11。但我不知道如何翻译成C驱动程序。我试过这个:
bson_init( &query );
bson_append_start_object( &query, "TimeStampSec" );
bson_append_int( &query, "$lt", 10 );
bson_append_finish_object( &query );
bson_append_int( &query, "node_id", 1 );
bson_finish( &query );
但是,它不起作用。我应该创建另一个对象吗?
答案 0 :(得分:0)
我对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 );
答案 1 :(得分:0)
这适合我。
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 );
}