how to process logical operation between 2 BSONObj precreated in mongodb -
i'm new mongodb. want execute query (a=3 or b=5 condition) using c++ driver..
i can create separately 2 bsonobj objects each a=3 , b=5 through bsonobjbuilder follows.
mongo::bsonobjbuilder builder = new mongo::bsonobjbuilder; mongo::bsonobjbuilder inner = new bsonobjbuilder(builder->subobjstart(condition)); inner->append("$gte", value); inner->append("$lte", value); mongo::bsonobj obj = builder.obj();
so have 2 different bsonobj. , can run them successfully. how can combine these objects using $or or $and logical operators.
assuming goal query {$or: [{a: 3}, {a: 5}]}
, construct mongodb c++ driver follows:
mongo::bsonobjbuilder querybuilder; mongo::bsonarraybuilder orclause(querybuilder.subarraystart("$or")); mongo::bsonobjbuilder firstclause(orclause.subobjstart()).append("a", 3).done(); mongo::bsonobjbuilder secondclause(orclause.subobjstart()).append("a", 5).done(); orclause.done(); mongo::bsonobj query(querybuilder.obj());
and here's less verbose way using or helper:
mongo::bsonobj query(or(bson("a" << 3), bson("a" << 5));
Comments
Post a Comment