[Mongo] group by time (formatted by time), mongo Group
The Group key can use the original field or a function to format the date.
/* 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")}...
Statistical Code:
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;}}});
Statistical results:
[ { "day" : "2014-8-19", "count" : 41 }, { "day" : "2014-8-22", "count" : 28 }, ...]
Reference: http://stackoverflow.com/questions/5168904/group-by-dates-in-mongodb
This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/39480359