设为首页 加入收藏

TOP

[Mongo]按时间分组统计(时间格式化)
2014-11-24 08:16:23 来源: 作者: 【 】 浏览:1
Tags:Mongo 时间 分组 统计 格式
分组的key可以使用原有的字段,也可以使用一个function来格式化日期。
/* 0 */
{
  "_id" : ObjectId("541fcc51c6c36038bc6b81cd"),
  "url" : "http://wifi21.com/",
  "addtime" : ISODate("2014-08-19T00:15:02Z")
}

/* 1 */
{
  "_id" : ObjectId("541fcc51c6c36038bc6b81ce"),
  "url" : "http://meiwen.me/src/index.html",
  "addtime" : ISODate("2014-08-19T00:15:07Z")
}
...

统计代码:

db.msds_accessrecord.group({
 keyf : function(doc){
	var date = new Date(doc.addtime);
	var dateKey = ""+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
	return {'day':dateKey}; //33
}, 
 initial : {"count":0}, 
 reduce : function Reduce(doc, out) {
	if(doc.url){
		out.count +=1;
	}
}
});

统计结果:

[
        {
                "day" : "2014-8-19",
                "count" : 41
        },
        {
                "day" : "2014-8-22",
                "count" : 28
        },
        ...
]

参考: http://stackoverflow.com/questions/5168904/group-by-dates-in-mongodb

本文出自 “orangleliu笔记本” 博客,请务必保留此出处 http://blog.csdn.net/orangleliu/article/details/39480359
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇描述 下一篇工具类之数据库工具类:DBUtil(..

评论

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

·如何从内核协议栈到 (2025-12-27 03:19:09)
·什么是网络协议?有哪 (2025-12-27 03:19:06)
·TCP/ IP协议有哪些 (2025-12-27 03:19:03)
·怎样用 Python 写一 (2025-12-27 02:49:19)
·如何学习python数据 (2025-12-27 02:49:16)