Executing a “GROUP BY” in RethinkDB

Because RethinkDB uses a map-reduce architecture underneath, the syntax for ‘group by’ is a little different than you might expect compared to SQL.

r.db('performance')
 .table('query_timings')
 .pluck('Query', 'Duration')
 .group('Query')
 .sum('Duration')

The interesting effect of this is that even though you specify only ‘Duration’ in the sum, you get the grouping fields back as well. If you remove ‘sum’, you’ll get all the rows in each group in an array, which I imagine would be pretty handy for a lot of React-based applications.

Leave a Reply

Your email address will not be published. Required fields are marked *