Mongodb中的查询操作比较丰富,支持逻辑操作,如:
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
在C++ driver中如何实现或查询呢?
源码包中的example中没有这样的例子,经过一番查找在bsonobjbuilder.h和jsobjtests.cpp找到相关代码
BSONObj actual() { return OR( BSON( "a" << GT << 1 << LTE << "x"), BSON( "b" << NE << 1 << NE << "f" << NE << 22.3), BSON( "x" << "p" ) ); }
inline BSONObj OR(const BSONObj& a, const BSONObj& b) { return BSON( "$or" << BSON_ARRAY(a << b) ); } inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c) { return BSON( "$or" << BSON_ARRAY(a << b << c) ); } inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d) { return BSON( "$or" << BSON_ARRAY(a << b << c << d) ); } inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e) { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e) ); } inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f) { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e << f) ); } 可以看出有两种方法
- 使用OR函数
- 使用$or + BSON_ARRAY构造
- 先看第一种方法:使用OR函数构造查询语句
mongo::BSONObj query_obj; query_obj = mongo::OR(BSON("n"<<max_v), BSON("a"<<"test1"), BSON("a"<<"test2")); auto_ptr<mongo::DBClientCursor> cursor = conn.query("test.c1", mongo::Query(query_obj));
查询时调用mongo::Query(BSONobj &obj)方法
- 再看第二种方法
auto_ptr<mongo::DBClientCursor> cursor = conn.query("test.c1", QUERY("$or" << BSON_ARRAY(BSON("n" << max_v) << BSON("a" << "test1") << BSON("a" << "test2"))));
查询时调用QUERY(X)宏
#define QUERY(x) ::mongo::Query( BSON(x) )
两种方法从代码来看,其实是一样的,只是外观不同,可以任意使用。
问题:如何动态的构造一个OR操作呢?
如:在一个数组里,有多个查询参数,在运行中可变,因此不能用写死的方法,调用宏在这种情况下不合适。既然宏能构造数组,那么宏所调用的函数必定支持动态数组的创建,因为对于C++driver来说,引用中创建的数组都是动态的!
方法如下:
vector<string> ids; ids.push_back("1001"); ids.push_back("1002"); ids.push_back("1003"); mongo::BSONArrayBuilder ab; for (vector<string>::iterator it = ids.begin(); it != ids.end(); ++it) { tmp = BSON("mid" << *it); ab << tmp; } mongo::BSONArray or_array; or_array = ab.arr(); auto_ptr<mongo::DBClientCursor> cursor = conn.query("test.c1", QUERY("$or" << or_array)); while (cursor->more()) { mongo::BSONObj p = cursor->next(); cout << p.getStringField("a") << endl; }
这里用到了BSONArrayBuilder类,它用于构造BSONArray类,构造完成后,调用arr()方法得到BSONArray对象。
BSONArrayBuilder与BSONObjBuilder都支持直接将想要的数据添加到Builder中
BSONArrayBuilder ab; ab << "item1"; ab << "item2"; ab << "item3"; BSONArray myArray = ab.arr(); # {[ "item1", "item2", "item3"] } BSONObjBuilder ob; ob << "key" << "item1"; BSONObj query = ob.obj(); # {"key": "item1"}
mongo::BSONObj obj1 = BSON("$or" << or_array); cout << obj1.jsonString() << endl;
运行结果为:
{ "$or" : [ { "mid" : "1001" }, { "mid" : "1002" }, { "mid" : "1003" } ] }
此时数组的构造问题解决了,还有一个问题,“有时候,需要在逻辑查询之后,再进行一次赛选,如何将逻辑操作与其它的查询条件结合呢?
根据BSON()的定义
#define BSON(x) (( ::mongo::BSONObjBuilder(64) << x ).obj())可知,经过BSON宏处理过后得到的是一个BSONOjb,BSONObj是缺少BSON构造功能的,只能通过BSONObjBuilder构造,需要用到如下两个函数
template<class T> inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<( T value ) { _builder->append(_fieldName, value); _fieldName = StringData(); return *_builder; } inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<( const BSONElement& e ) { _builder->appendAs( e , _fieldName ); _fieldName = StringData(); return *_builder; }注意到BSONObjBuilder的operation<<的参数是BSONElement 与 class T,经过测试,不直接支持BSONObj与BSONArray,即连个BSONObj不能直接相连
相关推荐
windows 下 MongoDB C++ 64位驱动,debug库文件,目前最新MongoDB C++ Driver 3.4.x版。
MongoDB C++ Driver 3.4.x 版,是目前最新版本。需要和MongoDBC驱动一起使用。
赠送jar包:mongodb-driver-core-4.2.3.jar; 赠送原API文档:mongodb-driver-core-4.2.3-javadoc.jar; 赠送源代码:mongodb-driver-core-4.2.3-sources.jar; 赠送Maven依赖信息文件:mongodb-driver-core-4.2.3....
MongoDB Java Driver 简单操作
mongodb c#驱动 最新驱动mongodb.driver.dll 版本2.12.0-beta1,包内有MongoDB.Bson.dll 、mongodb.driver.core.dll 等文件
赠送jar包:mongodb-driver-sync-4.2.3.jar; 赠送原API文档:mongodb-driver-sync-4.2.3-javadoc.jar; 赠送源代码:mongodb-driver-sync-4.2.3-sources.jar; 赠送Maven依赖信息文件:mongodb-driver-sync-4.2.3....
赠送jar包:mongodb-driver-sync-4.2.3.jar; 赠送原API文档:mongodb-driver-sync-4.2.3-javadoc.jar; 赠送源代码:mongodb-driver-sync-4.2.3-sources.jar; 赠送Maven依赖信息文件:mongodb-driver-sync-4.2.3....
java和mongodb连接,需要mongodb-driver,您还必须下载其依赖项: bson和 mongodb-driver-core》》3个包: mongodb-driver-3.8.2.jar; bson-3.8.2.jar; mongodb-driver-core-3.8.2.jar
mongodb-driver-core-3.5.0.jar,链接驱动
mongodb-driver-core-3.4.3,mongodb java开发常用组件。
mongodb-driver-3.4.3,mongodb java开发中常用组件。
mongoDB java driver api mongoDB java 驱动 所有类的api
赠送jar包:mongodb-driver-core-4.2.3.jar; 赠送原API文档:mongodb-driver-core-4.2.3-javadoc.jar; 赠送源代码:mongodb-driver-core-4.2.3-sources.jar; 赠送Maven依赖信息文件:mongodb-driver-core-4.2.3....
MongoDB针对C++驱动的安装
由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似...
mongodb-driver-3.4.0-rc1.jar mongodb-driver-3.4.0-rc1.jar mongodb-driver-3.4.0-rc1.jar mongodb-driver-3.4.0-rc1.jar
5. 开发 AbstractMongodbModule 类处理通用的 MongoDB 数据库表数据操作 数据库中不同的表都有自己的 AbstractMongodbModule 子类对应 6. 用 Perl 开发自动代码生成器,上层程序员对照数据库表结构写 .tmpl 配置...
mongodb-driver-core 4.3.3版本
mongodb-driver-3.0.3.jar的java连接驱动
MongoDB-C++-Driver3.2 动态库,含头文件,可直接连接使用,windows下动态链接库