2017-08-08 121 views
1

我試圖在此example之後使用mongodb與最新的C++驅動程序作爲參考。mongdb C++驅動程序編譯錯誤文檔{}

我的代碼如下:

#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 

任何想法如何解決這個問題?

+1

你如何編譯它?請提供一個gcc/clang的版本。 – sophros

+0

我用cmake使用clang版本(Apple LLVM版本8.1.0(clang-802.0.42))。 –

回答

1

由於您只向流提供了一個值,因此您沒有在正確的流上下文中傳遞finalize。要使用流構造器,你傳遞一個鍵,然後將其值:

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

給你一個結果對象持有代表JSON

{ 'k1' : v1, 'k2' : v2, ... 'kn' : vn }一個BSON對象

您的代碼提供只有一些關鍵字,所以文件流不處於finalize的接受狀態。

如果您的整個過濾器是JSON字符串,那麼只需將其轉換爲BSON bsoncxx::from_json即可。還要注意,基於流的文檔構建器或多或少地不重視,因爲這種混淆和誤用的機會。

您可以從basic建設者獲得更好的里程。

+0

謝謝。最近熟悉mongodb的東西。無法找到合適的文件和清晰度。 –

+0

@MupparthyRavindranath您是否閱讀過https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/和https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working -with-BSON /?我建議從這些開始。另外,https://mongodb.github.io/mongo-cxx-driver/api/current/ – acm

+0

中的doxygen文檔在應用您的建議後有效。實際上,我在「過濾器」中傳遞了一個json字符串,但現在我知道,它正在期待k << v。 –