差不多是抄官方的。
数据如下
{ "_id" : 0, "name" : "hexin0", "value" : 0, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 1, "name" : "hexin1", "value" : 1, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 2, "name" : "hexin2", "value" : 2, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 3, "name" : "hexin3", "value" : 3, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 4, "name" : "hexin4", "value" : 4, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 5, "name" : "hexin5", "value" : 5, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 6, "name" : "hexin6", "value" : 6, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 7, "name" : "hexin7", "value" : 7, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 8, "name" : "hexin8", "value" : 8, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 9, "name" : "hexin9", "value" : 9, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 10, "name" : "hexin10", "value" : 10, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 11, "name" : "hexin11", "value" : 11, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 12, "name" : "hexin12", "value" : 12, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 13, "name" : "hexin13", "value" : 13, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 14, "name" : "hexin14", "value" : 14, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 15, "name" : "hexin15", "value" : 15, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 16, "name" : "hexin16", "value" : 16, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 17, "name" : "hexin17", "value" : 17, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 18, "name" : "hexin18", "value" : 18, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 19, "name" : "hexin19", "value" : 19, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
需要实现下面sql :
select date as d_o_f , goup ,sum(value),count(*),avg(sum(value)/count(*))
from xx
where name like 'hexin%'
group by goup, date
1.定义分组的key
StringBuilder keyfun = new StringBuilder();
keyfun.append("function(d) {");
keyfun.append(" return { ");
keyfun.append(" goup : d.group ,");
keyfun.append(" d_o_f: d.date.getDay() ");
keyfun.append(" } ;");
keyfun.append(" }");
2. 遍历每个组的处理方式
StringBuffer reduce = new StringBuffer();
reduce.append("function ( curr, result) {");
reduce.append(" result.total += curr.value; ");
reduce.append(" result.count++;");
reduce.append("}");
3. 计算平均数
StringBuffer finalize = new StringBuffer();
finalize.append("function(result){");
finalize.append(" var weekdays = [ '星期天', '星期一', '星期二',");
finalize.append(" '星期三', '星期四', ");
finalize.append(" '星期五', '星期六' ];");
finalize.append(" result.d_o_f = weekdays[result.d_o_f]; ");
finalize.append(" result.avg = Math.round(result.total / result.count); ");
finalize.append("}");
4. 调用dao查询
Group group = Group.keyFunction(keyfun.toString()).initial("count", 0).initial("total", 0)
.reduce(reduce.toString()).finalizeFunction(finalize.toString());
//调用自己封装的dao来实现 , 并输出结果
List<Map<String, Object>> list = dao.group("c", Query.where("name").startWith("hexin"), group);
for (Map<String, Object> map : list) {
System.out.println(map);
}
封装的查询条件 :
{ "$regex" : { "$regex" : "^hexin" , "$options" : "m"}}
封装的group命令
{
"group": {
"$keyf": "function(d) { return { goup : d.group , d_o_f: d.date.getDay() } ; }",
"$reduce": "function ( curr, result) {result.total += curr.value; result.count++;}",
"initial": {
"total": 0,
"count": 0
},
"finalize": "function(result){
var weekdays = [ '星期天', '星期一', '星期二','星期三', '星期四','星期五', '星期六' ];
result.d_o_f = weekdays[result.d_o_f];
result.avg = Math.round(result.total / result.count);
}",
"ns": "c",
"cond": {
"name": {
"$regex": {
"$regex": "^hexin",
"$options": "m"
}
}
}
}
}
/查询结果
{goup=0.0, d_o_f=星期三, total=63.0, count=7.0, avg=9.0}
{goup=1.0, d_o_f=星期三, total=70.0, count=7.0, avg=10.0}
{goup=2.0, d_o_f=星期三, total=57.0, count=6.0, avg=10.0}