Question

I have a Collection containing a date field. I want to group it by dayOfMonth but at the time of projection I want to project the complete Date and associated count.

I have a raw Collection in mongodb containing a Timestamp (Date field) This is my Aggregation query:

db.raw.aggregate(
{
    "$match" : { "Timestamp":{$gte:new Date("2012-05-30T00:00:00.000Z"),$lt:new Date("2014-05-31T00:00:00.000Z")}}
},
{ 
    $group: 
    { 
        _id: { ApplicationId: "$ApplicationId", date: {$dayOfMonth: '$Timestamp'}   }, 
        count: { $sum: 1 } 
    }
}
)

In the above query I'm grouping with dayOfMonth but how can I project complete the Date with count?

Was it helpful?

Solution

Your "Timestamp" values are clearly actual points in time so there really isn't a "complete date" to return. You could just generally "do the math" based on the date range you are applying and the "day of month" values returned as you process the results returned.

But alternately you could just "apply the math" to the date values in order by rounding the "timestamp" values out to the day. The returned values are no longer date objects, but they are the millisecond since epoch values, so it is relatively easy to "seed" those to date functions:

db.raw.aggregate([
    { "$match" : { 
        "Timestamp":{
            "$gte": new Date("2012-05-30"), 
            "$lt": new Date("2014-05-31")
        }
    }},
    { "$group": {
        "_id": {
            "$subtract": [
                { "$subtract": [ "$Timestamp", new Date("1970-01-01") ] },
                { "$mod": [
                    { "$subtract": [ "$Timestamp", new Date("1970-01-01") ] },
                    1000 * 60 * 60 * 24
                ])
            ]
        },
        "count": { "$sum": 1 }
    }}
])

So when you subtract one date object from another the difference is milliseconds is returned as a number. So this just normalizes to epoch seconds by subtracting the epoch date. The rest is basic date math to round the result to the current day.

Alternately again you could just use other date aggregation operators and concatenate to a string, but there would be usually a bit more work involved unless those values were for direct use:

db.raw.aggregate([
    { "$match" : { 
        "Timestamp":{
            "$gte": new Date("2012-05-30"), 
            "$lt": new Date("2014-05-31")
        }
    }},
    { "$group": {
        "_id": {
            "$concat": [
                 { "$substr": [{ "$year": "$Timestamp" },0,4] },
                 "-",
                 { "$substr": [{ "$month": "$Timestamp" },0,2] },
                 "-",
                 { "$substr": [{ "$dayOfMonth": "$Timestamp" },0,2] }
            ]
        },
        "count": { "$sum": 1 }
    }}
])

OTHER TIPS

Neil Lunn has provides a great answer. Theirs one more approach that u can use :

db.raw.aggregate([
    {
        "$match" : 
            {   
                "Timestamp":{"$gte": new Date("2012-05-30"), "$lt": new Date("2014-07-31")}
            }
    },
    { 
        "$group" :
            {   
                "_id":{"$dayOfMonth": "$Timestamp"},
                "Date":{"$first":"$Timestamp"},
                "count": { "$sum": 1 }
            }
    }
])

It will return you date. Hope so this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top