Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New C++ Mongo driver: how to see type and how to get string value

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?

like image 793
Xauxatz Avatar asked Mar 10 '16 14:03

Xauxatz


2 Answers

  1. Printing Type as string (LIGNE 67)

    #include <bsoncxx/types.hpp>
    
    std::string bsoncxx::to_string(bsoncxx::type rhs);`
    
  2. element to std::string

    stdx::string_view view = e.get_utf8().value;
    string name = view.to_string();
    
    • stdx::string_view
    • std::string_view
like image 53
Setepenre Avatar answered Oct 20 '22 00:10

Setepenre


#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
like image 35
CMLDMR Avatar answered Oct 19 '22 23:10

CMLDMR