Welcome 微信登录

首页 / 数据库 / MySQL / MongoDB Primary---->编译MongoDB,C++连接MongoDB测试

C++ Language Center
点击打开链接


C++ driver download
点击打开链接
  1. Scons安装步骤:  
  2. cd build/scons  
  3. python setup.py install  
  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  
 
  1. 拷贝至 /usr/local/lib下  
  2. [root@:~/mongo-cxx-driver-v1.8]#cp libmongoclient.so /usr/local/lib  
  1. 安装 boost lib   
  2. ./bootstrap.sh  
  3. ./bjam install --prefix=/usr  
。。。。。。。。。。。。。。。。。。。。华丽分界线。。。。。。。。。。。。。。。。。。。。。。。。。
  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的文件  
。。。。。。。。。。。。。。。。。。。华丽的分界线。。。。。。。。。。。。。。
  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  

 
  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. }  
 
  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 << " printifage: ";  
  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. }  
Linux AS4下卸载单机Oracle 10.2.0.1数据库MongoDB Primary---->简要介绍 和 Linux安装MongoDB 并 简单使用相关资讯      MongoDB 
  • MongoDB 3.3.0 发布下载  (01月14日)
  • 使用MongoDB C#官方驱动操作  (12/31/2015 16:27:56)
  • CentOS 6.6下安装MongoDB 3.0.1  (12/21/2015 19:29:02)
  • MongoDB 3.2版WiredTiger存储引擎  (01月02日)
  • 进程监控工具Supervisor 启动  (12/26/2015 10:49:57)
  • MongoDB 3.2.1 RC0 发布下载  (12/18/2015 11:32:29)
本文评论 查看全部评论 (0)
表情: 姓名: 字数