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;
}
求高手帮忙。