实现案例:
//任务统计
@Action(value = "getTaskStatistic", results = { @Result(name = "success", type = "json", params = {
"includeProperties", "jsonResult" }) })
public String getTaskStatistic() {
DBObject initial = new BasicDBObject();
DBObject index = new BasicDBObject();
index.put("count", 0);
index.put("taskStatus", "");
initial.put("taskStatus", index);
String reduce = "function (doc, out) { "
+ " out.taskStatus.count = out.taskStatus.count+=1; "
+ " out.taskStatus.inspectStatus = doc.taskStatus;"
+ "}";
BasicDBList group = (BasicDBList) taskStatusService.group(new String[] { "taskStatus"}, null, initial, reduce, null);
this.jsonResult = group.toString();
return SUCCESS;
}
返回数据:
[ { "taskStatus" : { "count" : 4.0 , "taskStatus" : "Finished"}} , { "taskStatus" : { "count" : 3.0 , "taskStatus" : "Received"}} , { "taskStatus" : { "count" : 2.0 , "taskStatus" : "UnReceive"}}]