BSONObjBuilder Q;Q.append("Name", str);auto_ptr<DBClientCursor> cursor = conn.query("test".NewsContents", Q.obj());
The preceding statement can be used for query. Then, the conditions are appended.
The following lists several common statements:
1. <or>
BSONObj test = BSON("$lte"<<2011)Q.append("EntryTime", test);
2. Perform (between and) matching in the array [1, 2, 3, 4, 5, 6, 7, 8]
BSONObj test=BSON("$gte"<<"0000"<<"$lte"<<"0001");BSONObj TestElemMatch=BSON("$elemMatch"<<test);Q.append("Category", TestElemMatch);
3. (3 <A <= 4) or (A <= 3)
-> Shell:
db.test.find({"$or":[{"A":{$elemMatch:{"$gt":3,"$lte":4}}},{"A":{$elemMatch:{"$lte":4}}}]})
-> C ++
BSONObj ACon1=BSON("$gt"<<3<<"lte"<<4);BSONObj ACon2=BSON("$lte"<<3);BSONObj AElemMatch1=BSON("$elemMatch"<<ACon1);BSONObj AElemMatch2=BSON("$elemMatch"<<ACon2);BSONObj A1=BSON("A"<<AElemMatch1);BSONObj A2=BSON("A"<<AElemMatch2);vector<BSONObj> con;con.push_back(A1);con.push_back(A2);Q.append("$or",con);