MongoDB Primary---->编译MongoDB,C++连接MongoDB测试

C++ Language Center
点击打开链接


C++ driver download

点击打开链接


Scons安装步骤: cd build/scons python setup.py install

编译驱动之前需要安装pcre 和 scons [root@:~/mongo-cxx-driver-v1.8]#scons 经过一段时间的组建,生成libmongoclient.so: [root@:~/mongo-cxx-driver-v1.8]#ls authTest clientTest firstExample libmongoclient.a LICENSE.txt SConstruct whereExample client config.log httpClientTest libmongoclient.so mongo secondExample
拷贝至 /usr/local/lib下 [root@:~/mongo-cxx-driver-v1.8]#cp libmongoclient.so /usr/local/lib

安装 boost lib ./bootstrap.sh ./bjam install --prefix=/usr

。。。。。。。。。。。。。。。。。。。。华丽分界线。。。。。。。。。。。。。。。。。。。。。。。。。


另外如果你编译MongoDB的源码需要下载依赖包 ftp://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz make -f Makefile.ref JS_DIST=/usr make -f Makefile.ref export 编译mongoDB并install tar -xvf mongodb-src-r1.4.4.tar.gz cd mongodb-src-r1.4.4 scons --full install 另外如果你没有boost库 ,还需要安装boost | ./bootstrap.sh -> ./bjam install --prefix=/usr/local 所有安装完后,/usr/loca include 和 libl下会有相应的mongodb的文件

。。。。。。。。。。。。。。。。。。。华丽的分界线。。。。。。。。。。。。。。

1 .C++简单连接MongoDB #include <iostream> #include "mongo/client/dbclient.h" using namespace std; using namespace mongo; void run() { DBClientConnection c; c.connect("localhost"); //add port,c.connect("localhost:27017") } int main(void) { try { run(); cout<<"connected ok"<<endl; }catch(DBException& e){ cout<<"caught"<<e.what()<<endl; } return 0; } 编译: [root@:~/svn/mongoDB]#g++ main.cpp -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options 运行: [root@:~/svn/mongoDB]#./a.out connected ok
2.MongoDB自带的测试 #include <iostream> #include "mongo/client/dbclient.h" using namespace std; using namespace mongo; void run() { DBClientConnection c; c.connect("localhost"); //add port,c.connect("localhost:27017") } int main(void) { try { run(); cout<<"connected ok"<<endl; }catch(DBException& e){ cout<<"caught"<<e.what()<<endl; } return 0; }
#include <iostream> #include "mongo/client/dbclient.h" using namespace mongo; void printIfAge(DBClientConnection& c, int age) { auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") ); while( cursor->more() ) { BSONObj p = cursor->next(); cout << p.getStringField("name") << endl; } } void run() { DBClientConnection c; c.connect("localhost"); cout << "connected ok" << endl; BSONObj p = BSON( "name" << "Joe" << "age" << 33 ); c.insert("tutorial.persons", p); /**< 向person表中插入数据 */ p = BSON( "name" << "Jane" << "age" << 40 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Abe" << "age" << 33 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" ); c.insert("tutorial.persons", p); c.ensureIndex("tutorial.persons", fromjson("{age:1}")); cout << "count:" << c.count("tutorial.persons") << endl; /**< 显示person表中的数据数目 */ auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj()); while( cursor->more() ) { cout << cursor->next().toString() << endl; } cout << "\nprintifage:\n"; printIfAge(c, 33); } int main() { try { run(); } catch( DBException &e ) { cout << "caught " << e.what() << endl; } return 0; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值