【问题标题】:$exists query in mongodb c-api$exists 查询在 mongodb c-api
【发布时间】:2017-04-02 05:53:49
【问题描述】:

我正在尝试在 C 中实现以下 mongodb 查询

db.test.find({"timestamp": {"$exists":true}});

我以为会是这样的

bson query, existspart;
mongo_cursor cursor;
int i;

bson_init(&existspart);
bson_append_string ( &existspart, "$exists", "false" );
bson_finish(&existspart);
bson_init(&query);
bson_append_bson ( &query, "timestamp", &existspart );
bson_finish(&query);    

mongo_cursor_init(&cursor, conn, "mydb.test");
mongo_cursor_set_query(&cursor, &query );
while( mongo_cursor_next( &cursor ) == MONGO_OK )
{
    // blabla
} 

但它不起作用。我做错了什么?

【问题讨论】:

    标签: c mongodb bson mongodb-c


    【解决方案1】:

    替换

    bson_append_string ( &existspart, "$exists", "false" );
    

    bson_append_bool ( &existspart, "$exists", 1 );
    

    bson_append_bool ( &existspart, "$exists", 0 );
    

    如果您不希望该字段存在

    【讨论】:

      【解决方案2】:

      我使用了稍微不同的语法:

      bson_t* existspart = BCON_NEW("$exists", BCON_BOOL(true));
      bson_t *query = bson_new();
      BSON_APPEND_DOCUMENT(query, "timestamp", existspart);
      
      mongoc_cursor_t* cursor = MongoInterface::Instance().Find("db_name", query);
      
      const bson_t *doc;
      while (mongoc_cursor_next (cursor, &doc))
      {
        // Do stuff...
      }
      
      bson_destroy (existspart);
      bson_destroy (query);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 2010-12-08
        相关资源
        最近更新 更多