1

我读过可以在 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 );

但是,它不起作用。我应该创建另一个对象吗?

4

2 回答 2

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 );
    }
于 2013-03-20T09:50:44.647 回答
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 );

?

于 2012-12-20T13:40:04.360 回答