Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RethinkDB nested grouping

Tags:

rethinkdb

reql

I have a RethinkDB table where documents look something like this:

[
  { "date": Mon Oct 05 2015 19:30:00 GMT+01:00, "category": 1, "name": "Alice" },
  { "date": Wed Oct 07 2015 14:00:00 GMT+01:00, "category": 2, "name": "Ben" },
  { "date": Mon Nov 16 2015 12:30:00 GMT+01:00, "category": 1, "name": "Charles" },
  { "date": Sun Nov 01 2015 22:15:00 GMT+01:00, "category": 1, "name": "Donald" }
  (...)
]

I'm trying to write a query in order to group my data by month/year, then (within each group) by category, and then perform some aggregation on the subgroups.

Based on the example given in the ReQL docs (https://www.rethinkdb.com/api/javascript/group/), this gives the first step:

r.table("seances").group([r.row("date").month(), r.row("date").year()])

I'm stuck here. Chaining with group(r.row("category")) gives an error message (Cannot call group on the output of group). Calling ungroup before the second group doesn't work either.

Any idea how it can be done? (ReQL looks very powerful but quite different from what I'm accustomed to with lodash...).

like image 963
Nicolas Le Thierry d'Ennequin Avatar asked Jul 12 '16 10:07

Nicolas Le Thierry d'Ennequin


1 Answers

You can write sequence.group(FIRST_GROUPING).ungroup().merge(function(row) { return {reduction: row('reduction').group(SECOND_GROUPING).ungroup()}; }).

like image 155
mlucy Avatar answered Nov 15 '22 08:11

mlucy