I have the following function that gets a value out of a BSON
document when given a sequence of keys:
bsoncxx::document::element deepFieldAccess(bsoncxx::document::view bsonObj, const std::vector<std::string>& path) {
if (path.empty())
return {};
auto keysIter = path.begin();
const auto keysEnd = path.end();
auto currElement = bsonObj[*(keysIter++)];
while (currElement && (keysIter != keysEnd))
currElement = currElement[*(keysIter++)];
return currElement;
}
The returned bsoncxx::document::element
can hold a value of any type (int32
, document
, array
, utf8
, etc). How can I write this element to the console via std::cout
?
Ideally, I would just have to do:
bsoncxx::document::element myElement = deepFieldAccess(bsonObjView, currQuery);
std::cout << myElement << std::endl;