[英]Getting started with MongoDB C++ driver in Windows
嘗試在 Windows 7 中使用C++ driver
設置簡單的MongoDB
數據庫連接。 我使用Visual C++ compiler 19
對x86
, 32-bit MongoDB 3.0.6
, Boost 1_59_0
, Mongo legacy 1.0.5 C++ driver
。
驅動程序使用命令編譯OK
scons --cpppath=d:\boost_1_59_0 --libpath=d:\boost_1_59_0\stage\lib --msvc-host-arch=x86 install
程序是
#include <cstdlib>
#include <iostream>
using namespace std;
#include <WinSock2.h>
#include <windows.h>
#include "mongo/client/dbclient.h"
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
程序編譯使用
cl /EHsc /I"c:\mongo-cxx-driver-legacy-1.0.5\build\install\include" /I"d:\boost_1_59_0" /DSTATIC_LIBMONGOCLIENT mdb.cpp c:\mongo-cxx-driver-legacy-1.0.5\build\install\lib\libmongoclient-s.lib /link /LIBPATH:"D:\boost_1_59_0\stage\lib" ws2_32.lib
但是當我運行程序時,得到錯誤信息
無法連接無法初始化與localhost
連接,地址無效
server
運行正常,因為我可以通過shell
訪問它,添加記錄等。
這是我第一次編程MongoDB
,我有點卡住了。 有什么建議?
好的,問題解決了(感謝 stevepowell.ca/mongo-db-1.html)。 這是其他遇到此問題的人的答案:
Windows 需要在建立連接之前初始化客戶端。
#include <cstdlib>
#include <iostream>
#include <WinSock2.h>
#include <windows.h>
#include <memory>
#include "mongo/client/dbclient.h"
using namespace mongo;
using namespace std;
void run() {
mongo::client::initialize(); // this line is new
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
我希望這在教程中!
向前和向上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.