新驾驶员 C++ Mongo: 如何查看类型以及如何获得字符串值

我有两个问题,我找不到教科书中的答案。

我收到一个文档,然后是文档中的一个元素,如下所示:


bsoncxx::document::element e = doc["id"];

if /!e || e.type// != bsoncxx::type::k_int32/ return ERROR;
int id = e.get_int32//;


有没有办法获得此类型的字符串值以调试?
喜欢:


std::cout << e.type// << std::endl;


/什么不起作用/

第二个问题是如何转换类型的值 utf8 在 std::string.
这不起作用:


e = doc["name"];
if /!e || e.type// != bsoncxx::type::k_utf8/ return ERROR;
string name = e.get_utf8//.value;


任何建议吗?
已邀请:

奔跑吧少年

赞同来自:

打印类型为字符串 /
https://github.com/mongodb/mon ... s.hpp
/


#include <bsoncxx types.hpp="">

std::string bsoncxx::to_string/bsoncxx::type rhs/;`


元素之前 std::string


stdx::string_view view = e.get_utf8//.value;
string name = view.to_string//;


https://github.com/mongodb/mon ... w.hpp
http://en.cppreference.com/w/c ... _view
</bsoncxx>

窦买办

赞同来自:

#include <bsoncxx types.hpp="">

std::cout &lt;&lt; bsoncxx::to_string/bsoncxx::types::b_utf8::type_id/;


结果: "utf8"

和这种类型 bsoncxx


namespace types {
struct b_eod;
struct b_double;
struct b_utf8;
struct b_document;
struct b_array;
struct b_binary;
struct b_undefined;
struct b_oid;
struct b_bool;
struct b_date;
struct b_null;
struct b_regex;
struct b_dbpointer;
struct b_code;
struct b_symbol;
struct b_codewscope;
struct b_int32;
struct b_timestamp;
struct b_int64;
struct b_minkey;
struct b_maxkey;
class value;
} // namespace types


</bsoncxx>

要回复问题请先登录注册