MongoDb如何添加查询条件
解决方法:
MongoCollection<Document> collection = MongoDb.getDb().getCollection(collect_name);//collection查询数据库的全部集合,再根据条件筛选
BasicDBObject match = new BasicDBObject();
match.put(1,1);
BasicDBObject[] array = {new BasicDBObject("sendtimesec", new BasicDBObject("$gte",beforeDate)),new BasicDBObject("sendtimesec", new BasicDBObject("$lt",afterDate)) };
match.put("$and",array);
BasicDBObject where = new BasicDBObject();
where.put("$match", match);
List<BasicDBObject> list=new ArrayList<BasicDBObject>();
list.add(where);
AggregateIterable<Document> aggregate = collection.aggregate(list);
MongoCursor<Document> iterator = aggregate.iterator();
while(iterator.hasNext()){//x循环获取满足条件的记录
Document document = iterator.next();
}
本文链接:http://www.yayihouse.com/yayishuwu/chapter/1481