[Mongo] 按時間分組統計(時間格式化),mongo分組

來源:互聯網
上載者:User

[Mongo] 按時間分組統計(時間格式化),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





相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.