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

来源:互联网 发布:减肥励志故事知乎 编辑:程序博客网 时间:2024/06/10 21:16

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

转自:http://blog.csdn.net/crazyjixiang/article/details/6599840

C++ Language Center
点击打开链接


C++ driver download

点击打开链接


view plainprint?
  1. Scons安装步骤:  
  2. cd build/scons  
  3. python setup.py install  

view plainprint?
  1. 编译驱动之前需要安装pcre 和 scons  
  2. [root@:~/mongo-cxx-driver-v1.8]#scons  
  3. 经过一段时间的组建,生成libmongoclient.so:  
  4. [root@:~/mongo-cxx-driver-v1.8]#ls  
  5. authTest  clientTest  firstExample    libmongoclient.a   LICENSE.txt  SConstruct     whereExample  
  6. client    config.log  httpClientTest  libmongoclient.so  mongo        secondExample  

view plainprint?
  1. 拷贝至 /usr/local/lib下  
  2. [root@:~/mongo-cxx-driver-v1.8]#cp libmongoclient.so /usr/local/lib  

view plainprint?
  1. 安装 boost lib   
  2. ./bootstrap.sh  
  3. ./bjam install --prefix=/usr  

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


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

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

view plainprint?
  1. 1 .C++简单连接MongoDB  
  2. #include <iostream>  
  3. #include "mongo/client/dbclient.h"  
  4. using namespace std;  
  5. using namespace mongo;  
  6.   
  7. void run() {  
  8.    DBClientConnection c;  
  9.    c.connect("localhost"); //add port,c.connect("localhost:27017")  
  10. }  
  11.   
  12. int main(void)  
  13. {  
  14.    try {  
  15.        run();  
  16.        cout<<"connected ok"<<endl;  
  17.    }catch(DBException& e){   
  18.        cout<<"caught"<<e.what()<<endl;  
  19.    }  
  20.    return 0;  
  21.   
  22. }  
  23.   
  24. 编译:  
  25. [root@:~/svn/mongoDB]#g++ main.cpp -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options  
  26. 运行:  
  27. [root@:~/svn/mongoDB]#./a.out   
  28. connected ok  

view plainprint?
  1. 2.MongoDB自带的测试  
  2. #include <iostream>  
  3. #include "mongo/client/dbclient.h"  
  4. using namespace std;  
  5. using namespace mongo;  
  6.   
  7. void run() {  
  8.    DBClientConnection c;  
  9.    c.connect("localhost"); //add port,c.connect("localhost:27017")  
  10. }  
  11.   
  12. int main(void)  
  13. {  
  14.    try {  
  15.        run();  
  16.        cout<<"connected ok"<<endl;  
  17.    }catch(DBException& e){   
  18.        cout<<"caught"<<e.what()<<endl;  
  19.    }  
  20.    return 0;  
  21. }  

view plainprint?
  1. #include <iostream>  
  2. #include "mongo/client/dbclient.h"  
  3.   
  4. using namespace mongo;  
  5.   
  6. void printIfAge(DBClientConnection& c, int age) {  
  7.     auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );  
  8.     while( cursor->more() ) {  
  9.         BSONObj p = cursor->next();  
  10.         cout << p.getStringField("name") << endl;  
  11.     }  
  12. }  
  13.   
  14. void run() {  
  15.     DBClientConnection c;  
  16.     c.connect("localhost");   
  17.     cout << "connected ok" << endl;  
  18.     BSONObj p = BSON( "name" << "Joe" << "age" << 33 );  
  19.     c.insert("tutorial.persons", p); /**< 向person表中插入数据 */  
  20.     p = BSON( "name" << "Jane" << "age" << 40 );  
  21.     c.insert("tutorial.persons", p);  
  22.     p = BSON( "name" << "Abe" << "age" << 33 );  
  23.     c.insert("tutorial.persons", p);  
  24.     p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );  
  25.     c.insert("tutorial.persons", p);  
  26.   
  27.     c.ensureIndex("tutorial.persons", fromjson("{age:1}"));  
  28.   
  29.     cout << "count:" << c.count("tutorial.persons") << endl; /**< 显示person表中的数据数目 */  
  30.   
  31.     auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());  
  32.     while( cursor->more() ) {  
  33.         cout << cursor->next().toString() << endl;  
  34.     }  
  35.   
  36.     cout << "\nprintifage:\n";  
  37.     printIfAge(c, 33);  
  38. }  
  39.   
  40. int main() {  
  41.     try {  
  42.         run();  
  43.     }  
  44.     catch( DBException &e ) {  
  45.         cout << "caught " << e.what() << endl;  
  46.     }  
  47.     return 0;  
  48. }  
view plainprint?
  1. <pre name="code" class="cpp"><pre>