[英]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.