Using Javascript in Aggregation

97 views
Skip to first unread message

Chase

unread,
Sep 5, 2012, 11:02:05 AM9/5/12
to mongod...@googlegroups.com
Hi - Is it possible to use  javascript in the group by part of the aggregation framework?  Specifically I have statistic entry that has a date and I am trying to round these to the nearest minute, 5 minute, 15 minute, 1 hour interval so I can sum or average all values over a period. I have checked the mongo docs and it does not seem possible.

mathias

unread,
Sep 5, 2012, 11:44:57 AM9/5/12
to mongod...@googlegroups.com
Currently there are no plans to support js inside of the aggregation framework. If you really need that, you will have to use map/reduce.

You can use $minute / $hour, etc inside of a group to group by time. Doing the 5 and 15minute bucketing will be tricky, but should be possible using nested $cond (http://docs.mongodb.org/manual/reference/aggregation/#_S_cond). Please feel free to create a jira ticket to make time bucketing easier.

Also, you will need to do each bucket size as a separate aggregation as each $group is currently limited to a single key. I am considering lifting that restriction for 2.2, but not sure yet.

mathias

unread,
Sep 5, 2012, 11:51:49 AM9/5/12
to mongod...@googlegroups.com
Actually, you can use the technique descried here to bin on minutes: http://stackoverflow.com/questions/11611024/binning-and-tabulate-unique-count-in-mongo/11622749#11622749. Note that because you cannot currently do math directly on dates, you will need to first use $minute, etc to convert to numbers.

Jenna

unread,
Sep 5, 2012, 4:01:39 PM9/5/12
to mongod...@googlegroups.com
You may also want to check out this example, which groups by date buckets:
Reply all
Reply to author
Forward
0 new messages