I'm a bit confused by the docs (and examples and code) about how to extract values from bson. It looks to me like something like this ought to work, although [] returns a document::element and key() converts that to a string_view, and none of these things are the actual types I want.
mongocxx::collection collection = db["my_collection"];
mongocxx::cursor cursor =
collection.find(document{}
<< "my_id" << open_document << "$gt" << 285
<< close_document << finalize);
for (auto doc : cursor) {
// I expect the field _id to be an oid.
cout << doc["_id"].key() << " ";
// I expect the field my_id to be an integer.
cout << doc["my_id"].key() << " ";
// I expect the field animal to be a string.
cout << doc["animal"].key() << endl;
}
At another point in the docs, I feared I had to things like this, which reassuringly is wrong:
bsoncxx::to_json(document::element(doc["animal"]).get_value())
The compiler kindly gives me type errors, which lead me to interfaces to try, but I'm missing something. Any pointers?