//and、or多条件联合查询
//age条件(and条件)
BasicDBList condList = new BasicDBList();
BasicDBObject cond = new BasicDBObject();
cond.put("$gt",0);
cond.put("$lte",40);
BasicDBObject composeCod = new BasicDBObject();
composeCod.put("age", cond);
condList.add(composeCod);
//name条件
BasicDBObject nameCond = new BasicDBObject();
nameCond.put("name", "name");
condList.add(nameCond);
//查询条件组合(or条件)
BasicDBObject searchCond = new BasicDBObject();
searchCond.put("$or", condList);
DBCursor findRefResult = coll.find(searchCond);
int i=1;
while (findRefResult.hasNext()) {
System.out.println("Inserted Document: "+i);
System.out.println(findRefResult.next());
i++;
}
//排序操作
DBCursor cursor = collection.find(filter_dbobject).limit(10).sort(new BasicDBObject("create_time",-1));
时间: 2024-12-13 07:42:43