Some records recorded by date in development require statistics in various dimensions, such as day, month, year, and hour ,.. For Grouping statistics, some fields need to be de-duplicated. In the previous [Mongo] grouping statistics by time (group time formatting), group is used to achieve day-by-day statistics, however, the newDate () method may be a bit difficult. Today I read about aggreg.
Some records recorded by date in development require statistics in various dimensions, such as day, month, year, and hour ,.. For Grouping statistics, some fields need to be de-duplicated. In the previous [Mongo] grouping statistics by time (group time formatting), group is used to achieve day-by-day statistics, however, using the new Date () method may be a bit difficult. Today I read about aggreg.
Some records recorded by date in development require statistics in various dimensions, such as day, month, year, and hour ,.. For Grouping statistics, some fields need to be de-duplicated. In the previous [Mongo] grouping statistics by time (group time formatting), group is used to achieve day-by-day statistics, however, using the new Date () method may be a bit difficult. Today I have read about aggregate and used aggregation to write time statistics.
Tips.
The data structure is still:
/* 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")}...
Collect pv values by month (equivalent to group)
db.msds_accessrecord.aggregate([ {$group: { _id: { "$month": "$addtime" }, pv: {$sum: 1}} }, {$sort: {"_id": 1}}]);
Statistical results
/* 0 */{ "result" : [ { "_id" : 8, "pv" : 583 }, { "_id" : 9, "pv" : 1399 } ], "ok" : 1}
Calculate the url value on a monthly basis and remove the duplicate url. Here is just a demonstration. The statistics may be meaningless (equivalent to group + distinct)
db.msds_accessrecord.aggregate([ {$group: { _id: { "month": {"$month": "$addtime"}, "url": "$url" } }}, {$group: {_id:"$_id.month", uv: {$sum: 1}}}, {$sort: {"_id":1}}]);
Pipelines, sorting, and aggregation are used here.
Statistical results
/* 0 */{ "result" : [ { "_id" : 8, "uv" : 41 }, { "_id" : 9, "uv" : 134 } ], "ok" : 1}
Reference:
Aggregate usage: http://docs.mongodb.org/manual/reference/method/db.collection.aggregate/#db.collection.aggregate
Date Aggregate functions: http://docs.mongodb.org/manual/reference/operator/aggregation-date/
This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/39932081