2017-05-03 6 views
0

のWindows 7、msys2、コンパイラMinGWのは、C++コードからのMongoDBインスタンスに接続しようとすると、C++コード

からのMongoDBに接続できません。次のコマンドを使用してmongodを実行している:私は通常mongoによって及びRobomongoに、このデータベースに接続することができます

mongod --dbpath ./dev-db --bind_ip 127.0.0.1 --port 27017 

私は正常にコンパイルし、最後に安定したMongo C++ドライバをインストールしました。私は公式tutorialから取るコード:

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

mongocxx::uri uri("mongodb://127.0.0.1:27017"); 
mongocxx::client conn{uri}; 
auto db = conn["test"]; 

bsoncxx::document::value restaurant_doc = 
    document{} << "address" << open_document << "street" 
       << "2 Avenue" 
       << "zipcode" 
       << "10075" 
       << "building" 
       << "1480" 
       << "coord" << open_array << -73.9557413 << 40.7720266 << close_array 
       << close_document << "borough" 
       << "Manhattan" 
       << "cuisine" 
       << "Italian" 
       << "grades" << open_array << open_document << "date" 
       << bsoncxx::types::b_date{std::chrono::milliseconds{12323}} << "grade" 
       << "A" 
       << "score" << 11 << close_document << open_document << "date" 
       << bsoncxx::types::b_date{std::chrono::milliseconds{121212}} << "grade" 
       << "B" 
       << "score" << 17 << close_document << close_array << "name" 
       << "Vella" 
       << "restaurant_id" 
       << "41704620" << finalize; 
auto res = db["restaurants"].insert_one(std::move(restaurant_doc)) 

そして、これは、次のエラー提供:

This application has requested the Runtime to terminate it in an unusual way. 
Please contact the application's support team for more information. 
terminate called after throwing an instance of 'mongocxx::v_noabi::bulk_write_exception' 
    what(): No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve '127.0.0.1']: generic server error 

どのように私はこの問題を回避して、データベースに接続することができますか?

答えて

1

(同じIPに解決されます)の代わりに、それはlocalhostであることを変更してみてくださいあなたのコピーは、例の最初の行が欠落しています。

mongocxx::instance inst{};

http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/#make-a-connectionを参照してください。 mongocxx::instanceコンストラクタとデストラクタは、それぞれドライバを初期化してシャットダウンするため、ドライバを使用する前にmongocxx::instanceを作成し、ドライバが使用されている間は生き続ける必要があります。

0

127.0.0.1というDNSルックアップを試しているようです。

mongocxx::uri uri("mongodb://localhost:27017"); 
+0

localhostを使用しているのは役に立ちません。 '[localhost ']を解決できませんでした。 – Serbin

関連する問題