首页 > 代码库 > 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"));
		}
		*/
		
		
	}