使用C驱动程序在两个数据之间获取数据。

时间:2020-12-21 21:29:03

i inserted data like this

我像这样插入数据

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

{"userid": "manaf", "DataValue": {"$type": "00", "$binary": "sampleBinaryData"}, "timestamp": 1460718961132, "_id": {"$oid": " 5710cd7194831eea91v: "}

i need to get the data b/w timestamp values provided.

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

i allready done this by using below command in mongoDb client console.

我已经在mongoDb客户端控制台使用了下面的命令。

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

db.sampleCollection。查找({“时间戳”:{“$gte”:1460703944149,“$lt”:1460703944683},“userid”:“manaf”)

But i can't use like this in my c program.

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

This is my client program

这是我的客户程序

#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;
  }

i got error like this

我有这样的错误

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

What is the actual issue with this program?.

这个项目的实际问题是什么?

3 个解决方案

#1


0  

Probably need an $and query. If BCON is available, use it.

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

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

...

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

...

#2


0  

I know this is an old question, but I wanted to just chime in with some points that could be useful.

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

First off, I think this would probably be easier to visualize with BCON (basic documentation here), but I believe the specific error referenced in the question is due to the presence of the extra comma inside the timestamp document (this one: {"$gte":1460703944149, "$lt":1460703944683 }. That comma is fine in JSON, but it makes the macro fail because it thinks you're now sending in the next argument. You could overcome that with the technique described here, but I believe you'll still have issues once that's resolved, because it looks like the BSON_APPEND_UTF8 signature expects a pointer to your document, then the key, then the UTF8 string value. From the tutorial:

首先,我认为使用BCON(这里的基本文档)可以更容易地进行可视化,但是我认为问题中引用的特定错误是由于时间戳文档中存在额外的逗号(这个是:{“$gte”:1460703944149,“$lt”:1460703944683})。这个逗号在JSON中没有问题,但它会使宏失败,因为它认为您现在正在发送下一个参数。您可以使用这里描述的技术来克服这个问题,但是我相信一旦解决了这个问题,您仍然会遇到问题,因为看起来BSON_APPEND_UTF8签名需要指向文档的指针,然后是键,然后是UTF8字符串值。从本教程:

This example adds a call to BSON_APPEND_UTF8() to look for all documents matching {"hello" : "world"}...

这个示例向BSON_APPEND_UTF8()添加一个调用,以查找所有匹配{“hello”:“world”}的文档……

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

BSON_APPEND_UTF8(查询、“你好”、“世界”);

But, here, you'd want to set the timestamp field to a document, not a UTF8 value. The document that you would supply would have a $gte field and a $lt field set to your date/time bounds. You'll also need to add your userId field to your query document, and then you should have the finished query that you can send in.

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

With the bson_append_<insert datatype here> methods, I believe you could do something like the following to accomplish this (I have not tested this code, but I think it should be fairly close):

使用bson_append_ <在这里插入数据类型> 方法,我相信您可以做如下的事情来实现这一点(我还没有测试这段代码,但我认为应该非常接近):

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

or, using BCON:

或者,使用BCON:

bson_t *query;
query = BCON_NEW("timestamp", 
    "{", 
        "$gte", BCON_INT64(1460703944149),  
        "$lt", BCON_INT64(1460703944683), 
    "}",
    "userId", BCON_UTF8("manaf"),
);

#3


-1  

Can you change the arguments of macro BSON_APPEND_UTF8 look like this

可以将宏BSON_APPEND_UTF8的参数更改为如下所示吗

BSON_APPEND_UTF8 (query, "\"timestamp\": {\"$gte\":1460703944149, \"$lt\":1460703944683 }","\"userid\": \"manaf\"");

#1


0  

Probably need an $and query. If BCON is available, use it.

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

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

...

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

...

#2


0  

I know this is an old question, but I wanted to just chime in with some points that could be useful.

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

First off, I think this would probably be easier to visualize with BCON (basic documentation here), but I believe the specific error referenced in the question is due to the presence of the extra comma inside the timestamp document (this one: {"$gte":1460703944149, "$lt":1460703944683 }. That comma is fine in JSON, but it makes the macro fail because it thinks you're now sending in the next argument. You could overcome that with the technique described here, but I believe you'll still have issues once that's resolved, because it looks like the BSON_APPEND_UTF8 signature expects a pointer to your document, then the key, then the UTF8 string value. From the tutorial:

首先,我认为使用BCON(这里的基本文档)可以更容易地进行可视化,但是我认为问题中引用的特定错误是由于时间戳文档中存在额外的逗号(这个是:{“$gte”:1460703944149,“$lt”:1460703944683})。这个逗号在JSON中没有问题,但它会使宏失败,因为它认为您现在正在发送下一个参数。您可以使用这里描述的技术来克服这个问题,但是我相信一旦解决了这个问题,您仍然会遇到问题,因为看起来BSON_APPEND_UTF8签名需要指向文档的指针,然后是键,然后是UTF8字符串值。从本教程:

This example adds a call to BSON_APPEND_UTF8() to look for all documents matching {"hello" : "world"}...

这个示例向BSON_APPEND_UTF8()添加一个调用,以查找所有匹配{“hello”:“world”}的文档……

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

BSON_APPEND_UTF8(查询、“你好”、“世界”);

But, here, you'd want to set the timestamp field to a document, not a UTF8 value. The document that you would supply would have a $gte field and a $lt field set to your date/time bounds. You'll also need to add your userId field to your query document, and then you should have the finished query that you can send in.

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

With the bson_append_<insert datatype here> methods, I believe you could do something like the following to accomplish this (I have not tested this code, but I think it should be fairly close):

使用bson_append_ <在这里插入数据类型> 方法,我相信您可以做如下的事情来实现这一点(我还没有测试这段代码,但我认为应该非常接近):

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

or, using BCON:

或者,使用BCON:

bson_t *query;
query = BCON_NEW("timestamp", 
    "{", 
        "$gte", BCON_INT64(1460703944149),  
        "$lt", BCON_INT64(1460703944683), 
    "}",
    "userId", BCON_UTF8("manaf"),
);

#3


-1  

Can you change the arguments of macro BSON_APPEND_UTF8 look like this

可以将宏BSON_APPEND_UTF8的参数更改为如下所示吗

BSON_APPEND_UTF8 (query, "\"timestamp\": {\"$gte\":1460703944149, \"$lt\":1460703944683 }","\"userid\": \"manaf\"");