首页 > 代码库 > mongodb的group详细成功案例
mongodb的group详细成功案例
/** * 查询财务报表 * @author liupeng * @param timeStart * @param timeEnd * @return */ //public List<KeyRequest> findByTimeForFinal(String timeStart, String timeEnd) { public List<DBObject> findByTimeForFinal(String timeStart, String timeEnd) { BasicDBObject key = new BasicDBObject(); key.put("kq_timer_start", true); //groupBy的条件 BasicDBObject cond = new BasicDBObject("kq_timer_start",new BasicDBObject("$gte",timeStart).append("$lt", timeEnd));//查询where条件 BasicDBObject initial = new BasicDBObject(); initial.put("cou",0); //需要查询出来的内容 initial.put("kq_autosum", ""); //需要查询出来的内容 initial.put("kq_accountsum", ""); //需要查询出来的内容 String reduce = "function(obj,out){" + "out.cou++;" + "out.kq_autosum = out.kq_autosum+obj.kq_autosum+'_';" + "out.kq_accountsum = out.kq_accountsum+obj.kq_accountsum+'_';}"; BasicDBList returnList = (BasicDBList)keyRequest.group(key, cond, initial, reduce); System.out.println(returnList); List<DBObject> list = new ArrayList<DBObject>(); Object[] o = returnList.toArray(); for (int i = 0; i < o.length; i++) { DBObject dbObject = (DBObject)o[i]; list.add(dbObject); } return list; //通过以下形式可以打印出BasicDBList中的任何值 /* System.out.println(returnList); Object[] o = returnList.toArray(); for (int i = 0; i < o.length; i++) { DBObject dbObject = (DBObject)o[i]; System.out.println(dbObject.get("kq_id")); } */ }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。