I'm using MongoDB Aggregate pipeline and for most of the things I want to do, it's perfect so far.
However, I'd like to aggregate on time and group by individual days, hours or minutes. This is my query so far (in the case of minute granularity:
db.track.aggregate(
[
{$match: {event: /pricing/i}},
{$unwind:"$userId"},
{
$group: {
_id: {min: {$minute: "$timestamp"}},
count: {$sum: 1}
}
}
]
)
The obvious problem here is that it will group minutes of different hours together. like so:
{ "_id" : { "min" : 18 }, "count" : 8 }
{ "_id" : { "min" : 33 }, "count" : 18 }
{ "_id" : { "min" : 10 }, "count" : 6 }
{ "_id" : { "min" : 8 }, "count" : 2 }
{ "_id" : { "min" : 43 }, "count" : 2 }
{ "_id" : { "min" : 35 }, "count" : 6 }
{ "_id" : { "min" : 46 }, "count" : 2 }
{ "_id" : { "min" : 12 }, "count" : 4 }
{ "_id" : { "min" : 31 }, "count" : 4 }
{ "_id" : { "min" : 4 }, "count" : 14 }
I want to use the result of that query in a graph. Ideally, I would like to get back something like that:
{ "_id" : { "time" : "14:04" }, "count" : 14 }
My document look like this
{
_id: ObjectId("54cd7b8f7e4515a41898faac"),
userId: [
"xp1ungmsrh3hbhjk7c2go45xxvh0uiaa9rel5",
"a3c10b3c-3825-4b32-9a57-0e75b508d5bb"
],
type: "track",
timestamp: ISODate("2015-02-01T01:04:13.632Z"),
event: "View Pricing Page"
}
I'm sure I'm missing something obvious here but the doc doesn't give me anything else to go on.
Anyone can point me in the right direction?
Copyright Notice:Content Author:「Syl」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28266203/aggregate-query-on-time-in-mongodb