RethinkDB JavaScript Date Filter
Asked Answered
B

2

5

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?

Bright answered 30/9, 2015 at 19:22 Comment(0)
B
4

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.

Bright answered 5/10, 2015 at 17:51 Comment(0)
I
4

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:

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

Igal answered 1/10, 2015 at 2:31 Comment(4)
I was trying to avoid re-writing my script to use epoch_time, but I think as good practices I should just do it, so nice write up, but I'd already read all that, and certainly was running it in a server in a docker container that is managed in an EC2 instance in the amazon... but thanks.Bright
... also just because I was curious but pretty sure I tried you solution, my instincts were right... it's string data, needs to be cast to a date, and that breaks something somewhere along the way and poof magically doesn't workBright
Yeah...data type is the problem here. You have to fix it and convert it. But the way you write it makes me think you misunderstand ReQL. item.upload_date is invalid syntax to access field, item("upload_date") is how you write it.Igal
I figured that out as well... notice my second exampleBright
B
4

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.

Bright answered 5/10, 2015 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.