MongoDB C++ Driver 使用入门
来源:互联网 发布:数据审计追踪 编辑:程序博客网 时间:2024/05/16 12:34
最近在使用MongoDB的C++ Driver,很奇怪MongoDB是C++写的,但是却没有提供现成的C++ Driver,而且由于没怎么用过C++,自己去编译的过程中遇到好多问题,浪费好多天才编译成了一个库。简单的总结一下就是从源码编译出一个可以用的Driver需要以下几步:
- 下载MongoDB C++ Driver 源码 https://github.com/mongodb/mongo-cxx-driver
- 下载并安装scons,scons-2.3.0-1.noarch.rpm ( http://www.scons.org/download.php )
- 切换到MongoDB Driver源码目录下,执行命令 scons install,安装成功后会出现libmongoclient.a,就可以使用了。
然后就是用编译出来的库来进行数据库编程。具体命令如下:
g++ MongoTest.cpp -lmongoclient -L /home/xxx/MongoDB-CPP-Driver/mongo-cxx-driver-v2.4/src/ -I /home/xxx/MongoDB-CPP-Driver/mongo-cxx-driver-v2.4/src -lboost_thread-mt -lboost_thread -lboost_system -lboost_filesystem -lboost_program_options -L /usr/lib64/ -o MongoTest
这个过程看起来简单,但是自己基本没做过C++开发,所以遇到了好多坑,好在后来都解决了。编译的具体细节就不多说了,这不是本篇的重点,网上的例子也很多,请自行查找。
这次主要的目的是记录和分享一下几个基本的CRUD函数。在网上找MongoDB C++ Driver的API文档找了好久才找到,大家在写的时候可以参考API来写。地址:http://api.mongodb.com/cplusplus/2.4.0/namespaces.html
有了API文档写起来就方便多了,尽管很多地方还得靠猜靠试,但是进度已经比之前快多了。折腾了大概一天,把常用的几个接口,以及其中的每个参数都搞明白了,写完了个CRUD的简单例子。
连接数据库
static bool createConnection(){ bool flag; // create connnection if(!conn){ if (!conn.connect(DATABSE_IP +":"+ DATABSE_PORT),errmsg)) { throw Exception(errmsg); } flag = true; }else{ perror("Connection is created failed."); flag = false; } return flag; }
由于查遍了资料发现MongoDB的C++ Driver并不没有关闭连接的接口,后来网上看到网友分享说不需要关闭连接,很纳闷儿为什么不需要,自己没看过MongoDB的源码,在这里就不做深入研究了,总之连接不需要关闭,如果想保存连接,可以它提供的使用连接池,API文档里有介绍。
插入
static bool insertCollection(ServerInfo* si){ BSONObj p = BSONObjBuilder().append("ID", "1").append("Words", "Hello World").obj(); conn.insert(DATABASE_NAME+"."+COLLECTION_NAME, p);}
更新
static bool updateCollection(){ bool upsert = false; // update, if not exist, insert. bool updateMulti = false; // update multi BSONObj updateQuery = BSONObjBuilder().append("ID", "1").obj(); BSONObj updateData = BSONObjBuilder().append("Words", "This is MongoDB C++ Driver").obj(); con.update(DATABASE_NAME+"."+COLLECTION_NAME, updateQuery, updateData, upsert, updateMulti);}
删除
static bool deleteRecord(){ bool deleteOne = true; // delete only one record after selected. otherwise, delete all selected records. BSONObj deleteQuery = BSONObjBuilder().append("ID", "1").obj(); con.remove(DATABASE_NAME+"."+COLLECTION_NAME, deleteQuery, deleteOne);}
查询
static auto_ptr<DBClientCursor> getAllInfo(){ auto_ptr<DBClientCursor> dc = con.query(DATABASE_NAME+"."+COLLECTION_NAME); return dc;}
关于更详细的用法,更多的用法,可以参考上面连接里的API文档。因为网上这方面的例子非常少,而且都很乱,所以自己在这里总结分享一下,希望可以帮助到需要的人。
(完)
- MongoDB C++ Driver 使用入门
- Mongodb c Driver 使用实例
- mongoDB C Driver使用介绍
- Mongodb c Driver 编译
- mongodb c driver
- MongoDB Java Driver 使用
- mongodb安装和mongo-c-driver的安装使用
- 使用c++driver实现mongodb空间数据查询
- mongodb c driver的使用总结(1)
- 使用MongoDB C Driver时遇到的问题解决汇总
- MongoDb C#Driver 模糊查询
- Mongodb 安装c++driver Ubuntu
- MongoDB笔记四: C Driver
- MongoDB基本的操作及C接口使用-MongoDB C Driver
- MongoDB笔记一,Win10上使用C Driver访问MongoDB Server
- Mongodb matlab Driver 使用简介
- MongoDB入门——Driver安装篇
- mongodb C driver的异步查询
- vs2010缺少或未指定语言特性
- 【无人驾驶系列】基于ROS的无人驾驶系统
- HBase 常用Shell命令
- 关于提交AppStore的总结
- 构造方法
- MongoDB C++ Driver 使用入门
- Informix11.70培训笔记(2)
- 括号配对问题
- composer install中出现的用户名错误解决办法
- Ubuntu修改桌面大小
- c++: new, delete, malloc, free的匹配使用
- VS2010出现“为帮助内容存储区指定的位置无效或者您无权访问该位置”的解决办法
- CollapsingToolbarLayout简单应用
- Introducing Kafka Streams: Stream Processing Made Simple