首页 > 代码库 > Java中mongodb使用and和or的复合查询

Java中mongodb使用and和or的复合查询

条件类似:a=5 or (a=2 and b>3)
BasicDBList endList = new BasicDBList();

BasicDBObject forceEnd = new BasicDBObject();
forceEnd.put("status", 5);

BasicDBList condList = new BasicDBList();
BasicDBObject autoEnd = new BasicDBObject();
condList.add(new BasicDBObject("status", 2));
condList.add(new BasicDBObject("endTime", new BasicDBObject("$lt", new Date())));
autoEnd.put("$and",condList);

endList.add(forceEnd);
endList.add(autoEnd);

basicDBObject.put("$or",endList);

Java中mongodb使用and和or的复合查询