RethinkDB: Can I group by day / week / month?
Asked Answered
M

1

5

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.

Misdoing answered 11/2, 2015 at 23:26 Comment(0)
P
7

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()])
Phylum answered 12/2, 2015 at 0:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.