Group By Date in mongodb

I am using mongodb for tracking my website. I am saving date and time with ISODate , "time" : ISODate("2014-05-18T13:23:04.227Z") .

So, I wrote like that.

db.analystic.aggregate(
{
    "$project": {
        "y": {
            "$year": "$time"
        },
        "m": {
            "$month": "$time"
        },
        "d": {
            "$dayOfMonth": "$time"
        }
    }
},
{
    "$group": {
        "_id": {
            "year": "$y",
            "month": "$m",
            "day": "$d"
        },
        count: {
            "$sum": 1
        }
    }
},
{
    $sort: {
        "_id.year": 1,
        "_id.month": 1,
        "_id.day": 1
    }
})

First , need to separate the year , month and date. After that , make a group and then sort it.

comments powered byDisqus