7

我有两个问题在教程中找不到答案。

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

        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;

有小费吗?

4

2 回答 2

10
  1. 打印类型为字符串 ( LIGNE 67 )

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

    stdx::string_view view = e.get_utf8().value;
    string name = view.to_string();
    
于 2016-03-10T15:30:20.373 回答
1
#include <bsoncxx/types.hpp>

std::cout << 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
于 2017-08-03T07:56:42.497 回答