Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RethinkDB JavaScript Date Filter

I'm trying to query only things that are less than a day old... in JS this returns true; Why is rethink not returning true, so also not returning any results?

r.db( "db" ).table("table")
  .filter( function (item) {
    var
        now = new Date(),
        then = new Date( item.upload_date );
    return now - then > 1000*60*60*24
  });

I've tried this as well:

r.db( "db" ).table("table")
  .filter( function (item) {
    var
        now = new Date(),
        then = new Date( item( "upload_date") );
    return now - then > 1000*60*60*24
  });

EDIT: It's definitely my item( "upload_date" ) is not returning the date string... what should I be using there instead?

like image 630
Eric Hodonsky Avatar asked Sep 30 '15 19:09

Eric Hodonsky


2 Answers

I think you misunderstood ReQL. The filter function is supposed to be run on servers, not on your client.

You have to change it to this:

r.db( "db" ).table("table")
  .filter(r.now().sub(r.row('upload_date')).lt(60*60*24))

r.now() returns current time, assume that upload_date is stored in native RethinkDB time, you can sub tract two time value, the result is how many second elapses. Then you can compare with 60*60*24(how many seconds in a day)

Written in function style it will be

r.db( "db" ).table("table")
  .filter(function(item) {
     return r.now().sub(item('upload_date')).lt(60*60*24))
  })

It may confuse that the way it is written looks like raw JavaScript. But that's not the case. In fact, the client driver will evaluate function on client, to build the query, into a JSON string that RethinkDB understands and send it to the server. It doesn't run on client to execute logic of comparing. It generates a JSON string to tell RethinkDB what it wants using the API that is available on http://rethinkdb.com/api/javascript. More information about how driver works: http://rethinkdb.com/docs/writing-drivers/

Second, if you upload_date isn't in a native RethinkDB time object, you can try to convert it to RethinkDB time type for easily manipulation with some of these functions:

  • iso8601 time: http://rethinkdb.com/api/javascript/iso8601/
  • epoch_time: http://rethinkdb.com/api/javascript/epoch_time/

Or just try to post your time here and we will help you out.

like image 72
kureikain Avatar answered Nov 06 '22 00:11

kureikain


ReQL is smart enough to parse strings and sort them. If you have your date in the correct format it should still filter with an .lt on the row.

r.db("database").table("table")
    .filter( r.row('upload_date')
    .lt( new Date( new Date() - (24*60*60*1000) ).toISOString().replace(/\..{4}/, '').replace(/T/, ' ') ) )

--
Cheers

--EDIT--

( For learning or testing use Regex101.com )

Requested Regular Expression in String.replace() method:

FIRST - ( Dump the end of the time code )

eg: "2017-03-20T17:17:37.966Z" -> "2017-03-20T17:17:37"

  • Find the first literal period. \.
  • -> And ANY 4 characters after that.{4}

Replace the above group them with empty quote, or nothing

SECOND -

eg: "2017-03-20T17:17:37" -> "2017-03-20 17:17:37"

  • Find the first instance of the character T

Replace above group with a space character or " "

This sets the string to be able to compare inside rethink db

NOTICE: RethinkDB as a project has been dropped, but will be open sourced. Consider other resources for your projects if you require support.

like image 4
Eric Hodonsky Avatar answered Nov 06 '22 00:11

Eric Hodonsky