无法将元素添加到bsoncxx文档

时间:2019-04-27 17:43:55

标签: c++ mongodb mongo-cxx-driver

无法将元素添加到bsoncxx文档

    auto doc = bsoncxx::builder::basic::document{};

    const char* key = "asd";
    const char* value = "123";

    doc.append(kvp(key, value));
bsoncxx::v_noabi::builder::basic::sub_document::append_(bsoncxx::v_noabi::builder::concatenate_doc)': cannot convert argument 1 from '_Ty' to 'bsoncxx::v_noabi::builder::concatenate_doc'
1>          with
1>          [
1>              _Ty=std::tuple<const char *&,const char *&>
1>          ]

但此代码有效

    auto doc = bsoncxx::builder::basic::document{};

    const char* key = "asd";
    const char* value = "123";

    doc.append(kvp("asd", value));

mongo cxx驱动程序v3.3.1

2 个答案:

答案 0 :(得分:0)

您的初始代码不起作用,因为sub_document::append_的{​​{1}}没有专门化(模板只有enabled for std::stringstring_view)。

您的第二个示例确实有用,因为有一个for string literals

错误跟踪的以下部分提供了更多信息:

const char*

要使其正常工作,您可以将其作为std :: string传递(最好直接将键构建为std :: string):

1>    c:\mongo-cxx-driver\include\bsoncxx\v_noabi\bsoncxx\builder\basic\sub_document.hpp(46):
    note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>    c:\c++\workspace\test.cpp(207):
    note: see reference to function template instantiation 'void bsoncxx::v_noabi::builder::basic::sub_document::append<std::tuple<const char *&,const char *&>,>(Arg &&)' being compiled

如果您真的想使用auto doc = bsoncxx::builder::basic::document{}; const char* key = "asd"; // or: std::string keyStr("asd"); const char* value = "123"; doc.append(kvp(std::string(key), value)); // or: doc.append(kvp(keyStr,value)); ,可以在const char*上添加append_的专业化:

bsoncxx/builder/basic/sub_document.hpp

希望对您有帮助!

答案 1 :(得分:-1)

此代码有效

#include <bsoncxx/stdx/string_view.hpp>

    auto doc = bsoncxx::builder::basic::document{};

    bsoncxx::stdx::string_view key = "asd";
    const char* value = "123";

    doc.append(kvp(key, value));