c++ - 文档{}的mongodb C++驱动程序编译错误

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

我正在尝试将 mongodb 与最新的 c++ 驱动程序结合使用,遵循此 example作为引用。

我的代码如下:

#include <mongocxx/client.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;

class MongoDBApiUtils {

  public :
    MongoDBApiUtils(){}
    static json validateDoc(const std::string& key ,const json& regInfo);
    static json validatePreRegistration(const json& regInfo);
    static bool checkUserExist(const json& regInfo);
    static bool checkUserBlackList(const json& regInfo);


    /* 
     * Retrieves a latest block from blockchain, based on the 
     * given query field.  It is assumed that the database is
     * indexed on the queryField, to avoid O(n) problem.
     **/
    static json getLatestBlock(
          const mongocxx::database& db, const std::string& filter) {
      auto cursor = db["ctlblocks"].find_one(document{} << filter << finalize);
    }

    /** Creates and adds a new block into the blockchain **/
    static json addBlock(json& current, const json& prev) {

    }

  private :

};

#endif

但是我遇到编译错误,我无法破译。它通过调用 find_one 方法在我试图创建游标的行上给出错误。

In file included from /Volumes/second/nvi/github/blockchain_db/src/Impl/mongo/src/mongo_db_api.cpp:3:
/Volumes/second/nvi/github/blockchain_db/src/Impl/mongo/src/mongo_db_api_utils.hpp:47:46: error: no viable conversion from 'typename std::enable_if<!util::is_functor<const finalize_type &, void (single_context)>::value, key_context<closed_context> >::type' (aka 'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context>') to 'bsoncxx::document::view_or_value' (aka 'view_or_value<document::view, document::value>')
      auto cursor = db["ctlblocks"].find_one(document{} << filter << finalize);
                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/bsoncxx/v_noabi/bsoncxx/view_or_value.hpp:60:20: note: candidate constructor not viable: no known conversion from 'typename std::enable_if<!util::is_functor<const finalize_type &, void (single_context)>::value, key_context<closed_context> >::type' (aka 'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context>') to 'bsoncxx::v_noabi::document::view' for 1st argument
    BSONCXX_INLINE view_or_value(View view) : _view{view} {
                   ^   
/usr/local/include/bsoncxx/v_noabi/bsoncxx/view_or_value.hpp:69:20: note: candidate constructor not viable: no known conversion from 'typename std::enable_if<!util::is_functor<const finalize_type &, void (single_context)>::value, key_context<closed_context> >::type' (aka 'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context>') to 'bsoncxx::v_noabi::document::value &&' for 1st argument
    BSONCXX_INLINE view_or_value(Value&& value) : _value(std::move(value)), _view(*_value) {
                   ^   
/usr/local/include/bsoncxx/v_noabi/bsoncxx/view_or_value.hpp:75:20: note: candidate constructor not viable: no known conversion from 'typename std::enable_if<!util::is_functor<const finalize_type &, void (single_context)>::value, key_context<closed_context> >::type' (aka 'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context>') to 'const bsoncxx::v_noabi::view_or_value<bsoncxx::v_noabi::document::view, bsoncxx::v_noabi::document::value> &' for 1st argument
    BSONCXX_INLINE view_or_value(const view_or_value& other)
                   ^   
/usr/local/include/bsoncxx/v_noabi/bsoncxx/view_or_value.hpp:93:20: note: candidate constructor not viable: no known conversion from 'typename std::enable_if<!util::is_functor<const finalize_type &, void (single_context)>::value, key_context<closed_context> >::type' (aka 'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context>') to 'bsoncxx::v_noabi::view_or_value<bsoncxx::v_noabi::document::view, bsoncxx::v_noabi::document::value> &&' for 1st argument
    BSONCXX_INLINE view_or_value(view_or_value&& other) noexcept

有什么想法可以解决这个问题吗?

最佳答案

您不在正确的流上下文中以传递 finalize ,因为您只向流提供了一个值。要使用流生成器,您传递一个键,然后传递它的值:

auto result = document{} << k1 << v1 << k2 << v2 << ... kn << vn << finalize

为您提供一个包含表示 JSON 的 BSON 对象的结果对象

{ 'k1' : v1, 'k2' : v2, ... 'kn' : vn }

您的代码只提供了类似关键的东西,因此文档流未处于 finalize 的接受状态.

如果您的整个过滤器是一个 JSON 字符串,那么只需使用 bsoncxx::from_json 将其转换为 BSON .另请注意,由于存在这种混淆和误用的机会,基于流的文档构建器或多或少不再受到重视。

您可能会从 basic 获得更好的里程数 build 者。

关于c++ - 文档{}的mongodb C++驱动程序编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45572552/

相关文章:

c++ - 与 VS2013 相比,gcc 4.7.2 中的 std::map 实现效率非常低?

c++ - 为什么 PRIu64 在此代码中不起作用?

c++ - 如何编写 const 传播指针类型包装器?

linux - 无法在ubuntu中启动mongo shell

javascript - Meteorjs : How can I use variables which are defined in template events inside my template helpers?

node.js - 使用 Mongoose 进行架构投票的 "right way"?

c++ - 是否有 true 'smart tabs' 的任何 Visual Studio 加载项?

c++ - 在 Rcpp 中将 boost::array 转换为 NumericVector

c++ - 多态性中基类缺少虚拟析构函数 = 资源泄漏?

c++ - 如何重新安排并发任务?