7

チュートリアルで答えが見つからない 2 つの質問があります。

ドキュメントを取得し、次のようにドキュメントから要素を取得します。

        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;

(これは機能しません)

2 番目の質問は、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 に答える