[Mongo] group statistics time: aggregate, group, and distinct

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.