Mongo c driver: how to query documents with "_id" in the list?

I have a db with a huge amount of documents and I only want to request documents with "_id" from the list. I've searched the internet for hours and haven't found anything really working, so I'm posting my question here. Thank you so much for any help!

in MongoDB command line environment, it is very easy to achieve what I want. For example, the query command looks like this:

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

      

Using the mongoc driver to get a document by ID directly is easy 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);

      

To get documents with other keys other than "_id" the same can be easily achieved. 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);

      

However, when I try to put the _id in the array, I couldn't get it to work. I have tried two different ways. One of them:

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

      

Other:

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 return any document.

Again, thanks a lot for any suggestion, comment, in advance!


Edit:

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


Update:

Finally, I got a complete solution to my problem.

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

      

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

0


source to share


1 answer


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



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

      

0


source







All Articles