Filtering database entries by Author & between unix timestamp

78 views Asked by At

I've been stuck on trying to find a way to get all database entries from a specific author ID and between 2 timestamps. I've tried reading the documentation and messing around with the ways demonstrated and can't figure it out.

For example: getting all messages in the database from an author with id "281199286765748225" and between Unix timestamps of 1614920000 and 1614924579.

I've created a table within the "testing" database called "messages", here's an example layout: Example Database Entry

1

There are 1 answers

0
inf581 On

There are at least two ways. The most simple, without secondary-indexes, by using .filter:

r.db('testing').table('messages')
    .filter({author: '281199286765748225'})
    .filter(r.row('date').ge(1614920000))
    .filter(r.row('date').lt(1614924579))

And by using secondary-indexes, at first create index:

r.db('testing').table('messages')
    .indexCreate('author_date', [r.row("author"), r.row("date")])

then use .between:

r.db('testing').table('messages')
    .between(
        ['281199286765748225', 1614920000],
        ['281199286765748225', 1614924579],
        {index: 'author_date'}
    )