简体   繁体   English

MongoDB C-API中的$ exists查询

[英]$exists query in mongodb c-api

I am trying to implement the following mongodb query in C 我正在尝试在C中实现以下mongodb查询

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

I thought it would be something like 我以为会像

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
} 

But it does not work. 但这行不通。 What I am doing wrong? 我做错了什么?

replace 更换

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

with

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

or 要么

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

if you do not want that field exist 如果您不希望该字段存在

I used a slightly different syntax: 我使用了稍微不同的语法:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM