node.jsrethinkdbrethinkdb-javascript

Rethinkdb query to fetch data between 2 epoch times


I'm trying to run a ReQL query to fetch the data between 2 epoch times, i have a field called "lastUpdatedDttm" using which i'm trying to run the query like below. The lastUpdatedDttm field type is "number" and is just new date().getTime()

r.db('my_db').table('my_table').getAll('/mypath', { 
index: 'path' }) .filter(r.row('updatedBy').eq('id').and(r.row('lastUpdatedDttm').during(1512864000000, 1513209600000)))

The above query returns error, e: Not a TIME pseudotype:

Am i missing something in the query?


Solution

  • i believe duration needs time objects as arguments so you need to use the epochTime method to convert your times, so

    r.db('my_db').table('my_table').getAll('/mypath', { index: 'path' })
      .filter(
        r.row('updatedBy').eq(id)
          .and(
            r.row('lastUpdatedDttm')
              .during(
                r.epochTime(1512864000000 / 1000),
                r.epochTime(1513209600000 / 1000)
              )
          )
        )
    

    you should also make sure lastUpdatedDttm is stored as a date

    if you are storing lastUpdatedDttm as a number generated by new Date().getTime() you can do

    r.db('my_db').table('my_table').getAll('/mypath', { index: 'path' })
      .filter(record => {
        return record('updatedBy').eq(id).and(
          r.epochTime(record('lastUpdatedDttm').div(1000))
            .during(
              r.epochTime(1512864000000 / 1000),
              r.epochTime(1513209600000 / 1000)
            )
        )
    })