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"));
}
}
遇到了同样的问题,在CSDN看到了,希望阿里云团队能够给出正确、标准的答案~请查看
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。