oatpp / oatpp-mongo

Oat++ native BSON + MongoDB driver implementation based on Oat++ object-mapping sub-framework.
https://oatpp.io/
Apache License 2.0
6 stars 4 forks source link

collection.find_one query by ObjectId #7

Closed aurickislam closed 3 years ago

aurickislam commented 3 years ago

How can I query by "_id" : ObjectId?. For example:

auto result =
    collection.find_one(createMongoDocument( // <-- Filter
      oatpp::Fields<oatpp::String>({
        {"_id", "ObjectID(\"5f3258cfbaaccedaa5dd2d96\")"}
      })
    ));
lganzzzo commented 3 years ago

Hello @aurickislam ,

Take a look at this issue - https://github.com/oatpp/oatpp-mongo/issues/6

You'll need that objectIdFromString method:

auto result =
  collection.find_one(createMongoDocument( // <-- Filter
    oatpp::Fields({
      {"_id", objectIdFromString("5f3258cfbaaccedaa5dd2d96")}
    })
  ));

This is for now. Later, ObjectId will have a from-string constructor.

aurickislam commented 3 years ago

Thanks it worked. But needed to specify oatpp::Fields type to oatpp::Fields<oatpp::Any>

auto result =
    collection.find_one(createMongoDocument( // <-- Filter
      oatpp::Fields<oatpp::Any>({
        {"_id", objectIdFromString("5f3258cfbaaccedaa5dd2d96")}
      })
    ));
lganzzzo commented 3 years ago

Thanks it worked. But needed to specify oatpp::Fields type to oatpp::Fields

Yes, you are correct. Thanks for posting the correct answer!