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.

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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