mongodb - Mongo aggregation within intervals of time -
i have log data stored in mongo collection includes basic information request_id , time added collection, example:
{ "_id" : objectid("55ae6ea558a5d3fe018b4568"), "request_id" : "030ac9f1-aa13-41d1-9ced-2966b9a6g5c3", "time" : isodate("2015-07-21t16:00:00.00z") }
i wondering if use aggregation framework aggregate statistical data. counts of objects created within each interval of n minutes last x hours.
so output need 10 minutes intervals last 1 hour should following:
{ "_id" : 0, "time" : isodate("2015-07-21t15:00:00.00z"), "count" : 67 } { "_id" : 0, "time" : isodate("2015-07-21t15:10:00.00z"), "count" : 113 } { "_id" : 0, "time" : isodate("2015-07-21t15:20:00.00z"), "count" : 40 } { "_id" : 0, "time" : isodate("2015-07-21t15:30:00.00z"), "count" : 10 } { "_id" : 0, "time" : isodate("2015-07-21t15:40:00.00z"), "count" : 32 } { "_id" : 0, "time" : isodate("2015-07-21t15:50:00.00z"), "count" : 34 }
i use data graphs.
any advice appreciated!
something this?
pipeline = [ {"$project": {"date": { "year": {"$year": "$time"}, "month": {"$month": "$time"}, "day": {"$dayofmonth": "$time"}, "hour": {"$hour": "$time"}, "minute": {"$subtract": [ {"$minute": "$time"}, {"$mod": [{"$minute": "$time"}, 10]} ]} }} }, {"$group": {"_id": "$date", "count": {"$sum": 1}}} ]
example:
> db.foo.insert({"time": new date(2015, 7, 21, 22, 21)}) > db.foo.insert({"time": new date(2015, 7, 21, 22, 23)}) > db.foo.insert({"time": new date(2015, 7, 21, 22, 45)}) > db.foo.insert({"time": new date(2015, 7, 21, 22, 33)}) > db.foo.aggregate(pipeline)
and output:
{ "_id" : { "year" : 2015, "month" : 8, "day" : 21, "hour" : 20, "minute" : 40 }, "count" : 1 } { "_id" : { "year" : 2015, "month" : 8, "day" : 21, "hour" : 20, "minute" : 20 }, "count" : 2 } { "_id" : { "year" : 2015, "month" : 8, "day" : 21, "hour" : 20, "minute" : 30 }, "count" : 1 }
Comments
Post a Comment