gpt4 book ai didi

c - 使用 C Driver 获取两个日期之间的数据 mongoDb

转载 作者:可可西里 更新时间:2023-11-01 09:17:38 26 4
gpt4 key购买 nike

我这样插入数据

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

我需要获取提供的数据黑白时间戳值。

我已经通过在 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");

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

最佳答案

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

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

...

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

...

关于c - 使用 C Driver 获取两个日期之间的数据 mongoDb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com