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.

If you want a good Javascript book, I got a lot out of Secrets of the Javascript Ninja.

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 *