ServiceEntity里有userId、serviceTemplateId、id三列,我想根据userId、serviceTemplateId进行分组统计ServiceEntity的记录数,并返回userId、serviceTemplateId、count且能分页,如下是我写的代码,发现实现不了我的需求:
public List<ServiceEntity> listActive(ServiceEntity serviceEntity,
Pagination pagination) {
DBObject key = new BasicDBObject();
key.put("userId", true);
key.put("serviceTemplateId", true);
DBObject cond = new BasicDBObject();
cond.put("userId", serviceEntity.getUserId());
DBObject initial = new BasicDBObject();
initial.put("count", 0);
String reduce = "function(doc,out){out.count++;}";
String finalize = "function(doc){}";
DBObject groupResult = mongoTemplate.getCollection("ServiceEntity")
.group(key, cond, initial, reduce, finalize);
System.out.println(groupResult.toString());
return null;
}
求高手帮忙。
本文探讨了在MongoDB中如何根据userId和服务模板ID进行分组统计的问题,提供了一段尝试性的代码示例并指出了其中存在的问题。
1万+

被折叠的 条评论
为什么被折叠?



