新的C ++ Mongo驱动程序:如何查看类型以及如何获取字符串值 [英] New C++ Mongo driver: how to see type and how to get string value

查看:70
本文介绍了新的C ++ Mongo驱动程序:如何查看类型以及如何获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题,无法在本教程中找到答案.

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;

(无效)

第二个问题是如何将utf8类型值转换为std :: string. 这不起作用:

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;

有什么提示吗?

推荐答案

  1. 以字符串形式打印类型( LIGNE 67 )

#include <bsoncxx/types.hpp>

std::string bsoncxx::to_string(bsoncxx::type rhs);`

  • std :: string的元素

  • element to std::string

    stdx::string_view view = e.get_utf8().value;
    string name = view.to_string();
    

    • stdx :: string_view
    • std :: string_view
      • stdx::string_view
      • std::string_view
      • 这篇关于新的C ++ Mongo驱动程序:如何查看类型以及如何获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆