c - 蒙戈 : Error converting JSON to BSON

标签 c mongodb gcc mongo-c-driver

我正在尝试使用 Mongo C 驱动程序将以下 MongoDB JSON 命令转换为有效的 BSON:

db.test.update({
   "_id" : ObjectId("5624200d4bacd3940b8b2d62"),
  "folders.folder_id": "3_root",
  "folders.files": { $elemMatch: { "file": "5BD252AD-10C9-4DCE-A59C-5E3223BDDC60"}} },
  {$inc : { "folders.0.files.$.favorites.0.like": 1} }
);

我尝试使用以下方法创建它:

query = BCON_NEW ("_id", BCON_OID(&oid));
BSON_APPEND_UTF8 (query, "folders.folder_id", folderPositionRaw);
BSON_APPEND_UTF8 (query, "folders.files", "{" , "$elemMatch","{","file","5BD252AD-10C9-4DCE-A59C-5E3223BDDC60","}","}");

update = BCON_NEW ("$inc", 
    "{", 
    "folders.0.files.$.favorites.0.like", 1,
    "}");
mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error);

但这是错误的。

当我编译时,我得到:

/current/set_fav.c: In function ‘main’:
/current/set_fav.c:102: warning: initialization discards qualifiers from pointer target type
/current/set_fav.c:168:122: error: macro "BSON_APPEND_UTF8" passed 9 arguments, but takes just 3
/current/set_fav.c:168: error: ‘BSON_APPEND_UTF8’ undeclared (first use in this function)
/current/set_fav.c:168: error: (Each undeclared identifier is reported only once /current/set_fav.c:168: error: for each function it appears in.)

仅供引用:folderPositionRaw3_root 值在代码中设置得更高。

<小时/>

添加更多信息:最初我的 Mongo 语法是错误的,但 C 很高兴。我使用的是以下内容:

query = bson_new ();
bson_oid_init_from_string (&oid, tribe_id);
query = BCON_NEW ("_id", BCON_OID(&oid));       
BSON_APPEND_UTF8 (query, "folders.folder_id", folderPositionRaw);
BSON_APPEND_UTF8 (query, "folders.folder_id.$.files.file", file);

我得到了数据库团队的帮助,为我提供了正确的 Mongo 语法(帖子顶部),在我尝试将其构造为 C BSON 时,我做错了一些事情。

<小时/>

一些进展:

我已经更新了我的代码,它们不再崩溃,这是一个好兆头,但是发送到 Mongo 的输出 JSON 的结构不完全正确。我认为问题是在 mongo $inc 命令中发送的值 1 作为标量 1 而不是 int 1 发送。我已经尝试过在 1 周围带引号和不带引号。带引号的代码会运行,但不会运行执行更新(没有返回错误)。如果我删除引号,应用程序就会崩溃。

query = bson_new ();
bson_oid_init_from_string (&oid, tribe_id);

query = BCON_NEW ("_id", BCON_OID(&oid), "folders.folder_id", folderPositionRaw,"folders.files", 
    "{",
        "$elemMatch",
            "{",
                "file", file,
            "}",
    "}");       

// Find the document
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

// update document
update = BCON_NEW ("$inc", 
    "{", 
        "folders.0.files.$.favorites.0.like","1",
    "}");

    str = bson_as_json (update, NULL);
    printf ("***-> %s <-***\n\n",str);

mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error);

最佳答案

发现问题了!

您不能使用标准int,它必须是 BCON_INT

update = BCON_NEW ("$inc", 
    "{", 
        "folders.0.files.$.favorites.0.like",BCON_INT32 (1),
    "}");

:)

关于c - 蒙戈 : Error converting JSON to BSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36812281/

相关文章:

c - 如何在 Linux 中的进程之间交换二进制数据

c++ - 一种从中央存储库加载 DLL 的方法

c - 如何避免 C GTK 中的内存泄漏(使用 libxml)

java - 使用 Java MongoDb 驱动程序的 Bson pretty-print

node.js - 使用 mongoose 从 NodeJS 后端连接 MongoDB 错误

java - R - 重新格式化列表

gcc - 在 travis CI 上运行 Makefile 时找不到获取 gcc 命令

通过套接字的 C 服务器-客户端通信 - 如何从服务器向客户端打印消息

c - 海合会: "warning: assignment from incompatible pointer type"

java - Mongodb 在 Java 驱动程序中插入 $current Date