BasicDBObject key = new BasicDBObject();
key.put("areaCode", 1);
//条件
BasicDBObject cond = new BasicDBObject();
//初始化
BasicDBObject initial = new BasicDBObject();
initial.append("total", 0);
//reduce
String reduce = "function Reduce(doc, out) { " +
" out.total+=doc.caseKey;" +
"}";
BasicDBList groupList = (BasicDBList) mongoTemplate.getCollection(collectionName).group(key, cond, initial, reduce);
if (groupList != null && groupList.size() > 0) {
System.out.println("areaCode total");
for (int i = 0; i < groupList.size(); i++) {
BasicDBObject obj = (BasicDBObject) groupList.get(i);
System.out.println(obj.get("areaCode") + "" + obj.get("total"));
}
}