Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RethinkDB: Can I group by day / week / month?

Tags:

rethinkdb

My table has a timestamp field, which is a standard RethinkDB date field. Can I group rows by day / week / month using this timestamp field? Couldn't find any examples in the group() docs.

like image 919
Kludge Avatar asked Feb 11 '15 23:02

Kludge


1 Answers

To group by month, day, or day of week individually:

r.table(...).group(r.row('timestamp').month())
r.table(...).group(r.row('timestamp').day())
r.table(...).group(r.row('timestamp').dayOfWeek())

Grouping by the week isn't as easy at the moment, because ReQL is currently missing a function to get the week number from a data (see https://github.com/rethinkdb/rethinkdb/issues/2055 ). You can probably work around that by using some custom JavaScript code through r.js(). Please let me know if this is important for you, so I can look into it.

If you want to group by combinations of multiple things, e.g. day and month:

r.table(...).group([r.row('timestamp').month(), r.row('timestamp').day()])
like image 61
Daniel Mewes Avatar answered Sep 24 '22 12:09

Daniel Mewes