4

我插入了这样的数据

{“userid”:“manaf”,“DataValue”:{“$type”:“00”,“$binary”:“sampleBinaryData”},“timestamp”:1460718961132,“_id”:{“$oid”:“ 5710cd7194e5f57831eea91e”},“__v”:0}

我需要获取提供的数据 b/w 时间戳值。

我已经通过在 mongoDb 客户端控制台中使用以下命令来完成此操作。

db.sampleCollection.find({"timestamp": {"$gte":1460703944149, "$lt":1460703944683 },"userid": "manaf"})

但我不能在我的 c 程序中这样使用。

这是我的客户程序

#include <bson.h>
  #include <mongoc.h>
  #include <stdio.h>

  int
  main (int   argc,
        char *argv[])
  {
      mongoc_client_t *client;
      mongoc_collection_t *collection;
      mongoc_cursor_t *cursor;
      const bson_t *doc;
      bson_t *query;
      char *str;

      mongoc_init ();

      client = mongoc_client_new ("mongodb://localhost:27017/");
      collection = mongoc_client_get_collection (client, "sampledb", "sampleCollection");
      query = bson_new ();
      BSON_APPEND_UTF8 (query, "timestamp": {"$gte":1460703944149, "$lt":1460703944683 },"userid": "manaf");

      cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
      printf("Started\n");
            while (mongoc_cursor_next (cursor, &doc)) {
          str = bson_as_json (doc, NULL);
          printf ("%s\n", str);
          bson_free (str);
      }

      bson_destroy (query);
      mongoc_cursor_destroy (cursor);
      mongoc_collection_destroy (collection);
      mongoc_client_destroy (client);
      mongoc_cleanup ();

      return 0;
  }

我有这样的错误

error: macro "BSON_APPEND_UTF8" passed 4 arguments, but takes just 3
       BSON_APPEND_UTF8 (query, "timestamp": {"$gte":1460703944149, "$lt":1460703944683 },"userid": "manaf");

这个程序的实际问题是什么?

4

3 回答 3

0

可能需要一个 $and 查询。如果 BCON 可用,请使用它。

#include <bson.h>
#include <bcon.h>

...

    query = BCON_NEW("$and","[",
                     "{", "timestamp", "{", "$gte", BCON_INT64(1460703944149), "}", "}",
                     "{", "timestamp", "{", "$lt", BCON_INT64(1460703944683), "}", "}","]");

...
于 2017-02-15T21:30:44.167 回答
0

我知道这是一个老问题,但我想补充一些可能有用的观点。

首先,我认为这可能更容易用 BCON 可视化(基本文档here),但我相信问题中引用的具体错误是由于timestamp文档中存在额外的逗号(这个:{"$gte":1460703944149, "$lt":1460703944683 }.那个逗号是在 JSON 中很好,但它使宏失败,因为它认为你现在正在发送下一个参数。你可以用这里描述的技术克服这个问题,但我相信一旦解决了你仍然会遇到问题,因为它看起来像BSON_APPEND_UTF8 签名需要一个指向您的文档的指针,然后是密钥,然后是 UTF8 字符串值。来自教程

此示例添加对 BSON_APPEND_UTF8() 的调用以查找与 {"hello" : "world"} 匹配的所有文档...

BSON_APPEND_UTF8 (query, "hello", "world");

但是,在这里,您希望将timestamp字段设置为document,而不是 UTF8 值。您将提供的文档将有一个$gte字段和一个$lt设置为您的日期/时间范围的字段。您还需要将您的userId字段添加到您的查询文档中,然后您应该可以发送完成的查询。

使用这些bson_append_<insert datatype here>方法,我相信您可以执行以下操作来完成此操作(我尚未测试此代码,但我认为它应该相当接近):

bson_t *query;
bson_t timestampDoc;

query = bson_new();

BSON_APPEND_DOCUMENT_BEGIN(query, "timestamp", &timestampDoc);
bson_append_date_time(&timestampDoc, "$gte", -1, 1460703944149);
bson_append_date_time(&timestampDoc, "$lt", -1, 1460703944683);
bson_append_document_end(query, &timestampDoc);
BSON_APPEND_UTF8(query, "userId", "manaf");

或者,使用 BCON:

bson_t *query;
query = BCON_NEW("timestamp", 
    "{", 
        "$gte", BCON_INT64(1460703944149),  
        "$lt", BCON_INT64(1460703944683), 
    "}",
    "userId", BCON_UTF8("manaf"),
);
于 2017-07-22T22:54:37.477 回答
-1

你能改变宏 BSON_APPEND_UTF8 的参数看起来像这样吗

BSON_APPEND_UTF8 (query, "\"timestamp\": {\"$gte\":1460703944149, \"$lt\":1460703944683 }","\"userid\": \"manaf\"");
于 2016-04-18T07:22:41.277 回答