7

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?

fgalan
  • 11,732
  • 9
  • 46
  • 89
Xauxatz
  • 143
  • 2
  • 9
  • Did you have a look at the doxygen documentation for `bsoncxx::type` and `bsoncxx::element`? http://api.mongodb.org/cxx11/r3.0.0/annotated.html – acm Mar 11 '16 at 13:54

2 Answers2

10
  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();
    
Setepenre
  • 1,883
  • 3
  • 13
  • 16
1
#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
CMLDMR
  • 334
  • 2
  • 12