简体   繁体   English

新的 C++ Mongo 驱动程序:如何查看类型以及如何获取字符串值

[英]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.第二个问题是如何将utf8类型值转换为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 )打印类型为字符串( LIGNE 67

     #include <bsoncxx/types.hpp> std::string bsoncxx::to_string(bsoncxx::type rhs);`
  2. element to std::string元素到 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"结果:“utf8”

And these are type of bsoncxx这些是 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM