BCON for mongo-c-driver: Find _id

I managed to do something better with the C Driver, but got stuck at this simple point:

How do I search for a known ID? Among the many things, this is what I tried, which seemed the most logical:

query = BCON_NEW (
       "some_field", BCON_INT32(4),
       "_id", "{", 
           "$oid", "5414096132e0353007000017",
        "}"
    );

      

Query works fine if I don't include the _id field. With _id, it returns nothing and no errors. Of course, an entry with this _id exists in the db.

In the same topic, I am having difficulty forming the Ids for $ array in a query. Hope this helps too.

bson_t shiftIds;
BSON_APPEND_UTF8 (&shiftIds, "$oid", key); //Key is the shiftId string value, this goes in loop
query = BCON_NEW (
          "some_field", BCON_INT32(4),
          "shiftId", "{", 
            "$in", BCON_ARRAY(&shiftIds),
          "}"
     );

      

In this part, something goes wrong with the addition of $ oid to BSON_APEND_UTF8. The program ends when you reach there.

Any help is appreciated!

+3


source to share


2 answers


if you want to find the _id, maybe you can do it like this:



bson_oid_t oid;
bson_oid_init_from_string (&oid, "5414096132e0353007000017");
query = BCON_NEW ("_id", BCON_OID(&oid));

      

+3


source


An alternative solution to your first question:

bson_init_from_json(query, "{\"_id\":{\"$oid\":\"5414096132e0353007000017\"}}", -1, NULL);

      



Solution to second question: mongo c driver: how to request documents using "_id" in the list?

0


source







All Articles