Deep in MongoDB(C++ API)

BSONObjBuilder Q;
Q.append("Name", str);
auto_ptr<DBClientCursor> cursor = conn.query("test".NewsContents", Q.obj());

查询可以通过上面这句.然后条件分别append进去.

下面列出常见的几种语句:

1. << or >>
BSONObj test = BSON("$lte"<<2011)
Q.append("EntryTime", test);

2.在数组中进行(between and)匹配 [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);



原文链接: http://blog.csdn.net/crazyjixiang/article/details/6891883

你可能感兴趣的:(Deep in MongoDB(C++ API))