问题
I've got two questions that I cannot find an answer for in the tutorial.
I get a document and then an element from the doc like this:
bsoncxx::document::element e = doc["id"];
if (!e || e.type() != bsoncxx::type::k_int32) return ERROR;
int id = e.get_int32();
Is there a way to get a string value for the type, for debugging purposes? Like:
std::cout << e.type() << std::endl;
(which doesn't work)
The second question is how to convert the utf8 type value into a std::string. This doesn't work:
e = doc["name"];
if (!e || e.type() != bsoncxx::type::k_utf8) return ERROR;
string name = e.get_utf8().value;
Any tips?
回答1:
Printing Type as string (LIGNE 67)
#include <bsoncxx/types.hpp> std::string bsoncxx::to_string(bsoncxx::type rhs);`
element to std::string
stdx::string_view view = e.get_utf8().value; string name = view.to_string();
- stdx::string_view
- std::string_view
回答2:
#include <bsoncxx/types.hpp>
std::cout << bsoncxx::to_string(bsoncxx::types::b_utf8::type_id);
result: "utf8"
And these are type of 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
来源:https://stackoverflow.com/questions/35920013/new-c-mongo-driver-how-to-see-type-and-how-to-get-string-value