如何将 bsoncxx::document::element 写入控制台
How to write a bsoncxx::document::element to the console
我有 following function 在给定键序列时从 BSON
文档中获取值:
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;
}
返回的 bsoncxx::document::element
可以包含任何类型的值(int32
、document
、array
、utf8
等)。如何通过 std::cout
将此元素写入控制台?
理想情况下,我只需要这样做:
bsoncxx::document::element myElement = deepFieldAccess(bsonObjView, currQuery);
std::cout << myElement << std::endl;
bsoncxx::document::view
类型定义了到 JSON 的转换,通过 bsoncxx/json.hpp
[=] 中的函数 bsoncxx::to_json
表示为 std::string
18=]。单个 b_xyz
类型不提供此类转换,因为未包含在文档或数组结构中的元素不一定 JSON。您可以自由地构建自己的 to_string
函数,并为每个声明的 BSON 类型定义它。
我们无法打印 bsoncxx::document::element
,但我们可以打印 bsoncxx::document::view
。因此,只需将一个转换为另一个并清理生成的字符串。它丑陋且效率低下,但可以快速查找 bsoncxx::document::element
.
的值
std::string turnQueryResultIntoString(bsoncxx::document::element queryResult) {
// check if no result for this query was found
if (!queryResult) {
return "[NO QUERY RESULT]";
}
// hax
bsoncxx::builder::basic::document basic_builder{};
basic_builder.append(bsoncxx::builder::basic::kvp("Your Query Result is the following value ", queryResult.get_value()));
// clean up resulting string
std::string rawResult = bsoncxx::to_json(basic_builder.view());
std::string frontPartRemoved = rawResult.substr(rawResult.find(":") + 2);
std::string backPartRemoved = frontPartRemoved.substr(0, frontPartRemoved.size() - 2);
return backPartRemoved;
}
也许你是这个意思?
bsoncxx::document::element myElement = deepFieldAccess(bsonObjView, currQuery);
std::cout << myElement.get_utf8().value.to_string() << std::endl;
我有 following function 在给定键序列时从 BSON
文档中获取值:
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;
}
返回的 bsoncxx::document::element
可以包含任何类型的值(int32
、document
、array
、utf8
等)。如何通过 std::cout
将此元素写入控制台?
理想情况下,我只需要这样做:
bsoncxx::document::element myElement = deepFieldAccess(bsonObjView, currQuery);
std::cout << myElement << std::endl;
bsoncxx::document::view
类型定义了到 JSON 的转换,通过 bsoncxx/json.hpp
[=] 中的函数 bsoncxx::to_json
表示为 std::string
18=]。单个 b_xyz
类型不提供此类转换,因为未包含在文档或数组结构中的元素不一定 JSON。您可以自由地构建自己的 to_string
函数,并为每个声明的 BSON 类型定义它。
我们无法打印 bsoncxx::document::element
,但我们可以打印 bsoncxx::document::view
。因此,只需将一个转换为另一个并清理生成的字符串。它丑陋且效率低下,但可以快速查找 bsoncxx::document::element
.
std::string turnQueryResultIntoString(bsoncxx::document::element queryResult) {
// check if no result for this query was found
if (!queryResult) {
return "[NO QUERY RESULT]";
}
// hax
bsoncxx::builder::basic::document basic_builder{};
basic_builder.append(bsoncxx::builder::basic::kvp("Your Query Result is the following value ", queryResult.get_value()));
// clean up resulting string
std::string rawResult = bsoncxx::to_json(basic_builder.view());
std::string frontPartRemoved = rawResult.substr(rawResult.find(":") + 2);
std::string backPartRemoved = frontPartRemoved.substr(0, frontPartRemoved.size() - 2);
return backPartRemoved;
}
也许你是这个意思?
bsoncxx::document::element myElement = deepFieldAccess(bsonObjView, currQuery);
std::cout << myElement.get_utf8().value.to_string() << std::endl;