设为首页 加入收藏

TOP

mongodb的group简析(一)
2014-11-24 00:34:29 来源: 作者: 【 】 浏览:17
Tags:mongodb group 简析
mongodb的group简析
数据如下 www.2cto.com
{ "_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 :
1
select date as d_o_f , goup ,sum(value),count(*),avg(sum(value)/count(*))
2
from xx
3
where name like 'hexin%'
4
group by goup, date
www.2cto.com
1.定义分组的key
1
StringBuilder keyfun = new StringBuilder();
2
keyfun.append("function(d) {");
3
keyfun.append(" return { ");
4
keyfun.append(" goup : d.group ,");
5
keyfun.append(" d_o_f: d.date.getDay() ");
6
keyfun.append(" } ;");
7
keyfun.append(" }");
2. 遍历每个组的处理方式
1
StringBuffer reduce = new StringBuffer();
2
reduce.append("function ( curr, result) {");
3
reduce.append(" result.total += curr.value; ");
4
reduce.append(" result.count++;");
5
reduce.append("}");
3. 计算平均数
1
StringBuffer finalize = new StringBuffer();
2
finalize.append("function(result){");
3
finalize.append(" var weekdays = [ '星期天', '星期一', '星期二',");
4
finalize.append(" '星期三', '星期四', ");
5
finalize.append(" '星期五', '星期六' ];");
6
finalize.append(" result.d_o_f = weekdays[result.d_o_f]; ");
7
finalize.append(" result.avg = Math.round(result.total / result.count); ");
8
finalize.append("}");
4. 调用d
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MongoDB:mongodb的主从复制(二.. 下一篇机房收费系统之vb报表的模板的制..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: