2017-08-08 2 views
1

Ich versuche, mongodb mit neuesten C++ Treiber nach diesem example als Referenz verwenden.mongdb C++ Treiber Kompilierung Fehler für Dokument {}

Mein Code ist wie folgt:

#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 

Aber ich bekomme Fehler kompilieren, die ich nicht in der Lage bin zu entziffern. Es gibt einen Fehler in der Zeile, in der ich versuche, einen Cursor zu erstellen, indem ich die Methode find_one aufruft.

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 

Irgendwelche Ideen, wie man das löst?

+1

Wie kompilieren Sie es? Bitte geben Sie eine Version von gcc/clang an. – sophros

+0

Ich benutze die clang-Version (Apple LLVM Version 8.1.0 (clang-802.0.42)) mit cmake. –

Antwort

1

Sie befinden sich nicht im richtigen Stream-Kontext, um finalize zu übergeben, da Sie dem Stream nur einen Wert bereitgestellt haben. Um den Stream-Builder zu verwenden, übergeben Sie einen Schlüssel und dann seinen Wert:

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

Gibt Ihnen ein Ergebnisobjekt ein BSON Objekt hält, die die JSON

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

Ihr Code zur Verfügung gestellt nur etwas wie Schlüssel, so dass der Dokumentdatenstrom für finalize nicht in einem akzeptierenden Zustand ist.

Wenn Ihr gesamter Filter ein JSON-String ist, dann konvertieren Sie ihn einfach in BSON mit bsoncxx::from_json. Beachten Sie auch, dass die strombasierten Dokument-Builder mehr oder weniger de-betont werden, aufgrund genau dieser Art von Verwirrung und Missbrauch.

Sie können bessere Laufleistung von den basic Erbauer erhalten.

+0

Danke. Vor kurzem habe ich mich mit Mongodb-Sachen vertraut gemacht. Unfähig, geeignete Dokumentation und Klarheit zu finden. –

+0

@MupparthyRavindranath hast du https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/ und https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working gelesen -mit-bson /? Ich empfehle, mit diesen zu beginnen. Auch die doxigen Dokumente unter https://mongodb.github.io/mongo-cxx-driver/api/current/ – acm

+0

Funktioniert nach der Anwendung Ihres Vorschlags. Ich habe tatsächlich eine JSON-Saite im "Filter" übergeben, aber jetzt weiß ich, dass es k << v erwartet hat. –