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?
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();
#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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With