mongo C驱动程序:如何使用"_id"查询文档在列表中? [英] mongo c driver: how to query documents with "_id" in a list?

查看:154
本文介绍了mongo C驱动程序:如何使用"_id"查询文档在列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中包含大量文档,我只想从列表中查询带有"_id"的文档.我在网上搜索了几个小时,但没有找到任何真正有效的方法,因此我在此处发布了我的问题.非常感谢您的帮助!

I have a db with huge amount of documents, and I only want to query documents with "_id" from a list. I searched online for a few hours, and did not find anything really working, so I post my question here. Thank you very much for any help!

在MongoDB命令行环境中,很容易实现我想要的.例如,查询命令如下:

in MongoDB command line environment, it is very easy to achieve what I want. The query command, for example, is as follows:

db.collection.find({"_id":{$in:[ObjectId("595320c208b0c52a8b37c151"), ObjectId("595320c208b0c52a8b37c152"), ObjectId("595320c208b0c52a8b37c153")]}})

使用mongoc驱动程序,直接通过id检索文档也很简单.下面是一个示例:

Using mongoc driver, to retrieve document by id directly is simple too. Below is an example:

bson_t *qry = bson_new();
BSON_APPEND_OID(qry, "_id", &oid);
mongoc_cursor_t *cursor = mongoc_collection_find(col, MONGOC_QUERY_NONE,
                                                 0, 0, 0, qry, NULL, NULL);

要使用"_id"以外的其他键来获取文档,同样可以轻松实现.下面是一个示例:

To fetch documents with other keys other than "_id", same thing can be achieved easily too. Below is an example:

bson_t *qry = bson_new();
qry = BCON_NEW("$query", "{", "name", "{", "$in", "[",
               "Steve", "John", "Henry", "]", "}", "}");
mongoc_cursor_t *cursor = mongoc_collection_find(col, MONGOC_QUERY_NONE,
                                                 0, 0, 0, qry, NULL, NULL);

但是,当我尝试将_id放入数组时,我无法使其正常工作.我尝试了两种不同的方法.一个是:

However, when I try to put the _id's in the array, I could not get it work. I tried two different ways. One is:

bson_oid_t oid1;
bson_oid_init_from_string(&oid1, "595320c208b0c52a8b37c151");
bson_oid_t oid2;
bson_oid_init_from_string(&oid2, "595320c208b0c52a8b37c152");
bson_oid_t oid3;
bson_oid_init_from_string(&oid3, "595320c208b0c52a8b37c153");
qry = BCON_NEW("$query", "{", "_id", "{", "$in", "[",
               oid1, oid2, oid3, "]", "}", "}");
mongoc_cursor_t *cursor = mongoc_collection_find(col, MONGOC_QUERY_NONE,
                                                 0, 0, 0, qry, NULL, NULL);

另一个是:

qry = BCON_NEW("$query", "{", "_id", "{", "$in", "[",
               "ObjectId(\"595320c208b0c52a8b37c151\")",
               "ObjectId(\"595320c208b0c52a8b37c152\")",
               "ObjectId(\"595320c208b0c52a8b37c153\")", "]", "}", "}");
mongoc_cursor_t *cursor = mongoc_collection_find(col, MONGOC_QUERY_NONE,
                                                 0, 0, 0, qry, NULL, NULL);

它们都不返回任何文档.

None of them returns any document.

再次,非常感谢您提前提出任何建议,评论!

Again, thank you very much for any suggestion, comment, in advance!

我得到了我自己的原始问题的答案(请参见下面的答案).但是,如果要动态形成查询,换句话说,用例是从其他地方获取ID列表,在形成查询之前,我不知道ID是什么,我只知道它们会被传递给我的容器,例如std :: vector< std :: string> ;,如何动态形成此查询?

I got my original question answered myself (see the answer below). However, if I want to dynamically form the query, in other words, the use case is that I get the list of ids from somewhere else, before forming the query, I don't know what the ids are, I only know they will be passed to me in a container, say std::vector<std::string>, how can I form this query dynamically?

最后,我得到了解决问题的完整解决方案.

Finally, I got the complete solution to my problem.

bson_init_from_json(qry,
                    "{\"_id\":{\"$in\":
                     [{\"$oid\":\"595320c208b0c52a8b37c151\"}, 
                      {\"$oid\":\"595320c208b0c52a8b37c152\"}, 
                      {\"$oid\":\"595320c208b0c52a8b37c153\"}]}}",
                    -1,
                    NULL);

由于上述查询是由字符串构成的,因此可以动态更新,从而完全解决了我的问题!

As the above query is formed from a string, it can be updated dynamically, and thus completely solves my problem!

推荐答案

进一步调查后,我的问题得到了解答.我应该直接使用函数BCON_OID,而不是直接使用oid变量或使用字符串.以下查询有助于获取我想要的内容:

After some further investigation, I got my question answered myself. Instead of using oid variables directly or using strings, I should use the function BCON_OID. The following query helps fetch what I wanted:

qry = BCON_NEW("$query", "{", "name", "{", "$in", "[",
               BCON_OID(oid1), BCON_OID(oid2), BCON_OID(oid3), "]", "}", "}");

这篇关于mongo C驱动程序:如何使用"_id"查询文档在列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆