RethinkDB JavaScript Date Filter

I am trying to request only those things that are less than one day ... in JS this returns true; Why doesn't rethink return true, so it also returns no 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 also tried this:

 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: This is definitely my item( "upload_date" ) does not return a date string ... what should I use there instead?

+5
source share
2 answers

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

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

-
Greetings

- EDIT -

(Use Regex101.com to study or test)

The requested regular expression in the String.replace() method:

FIRST - (Dump the end of the time code)

for example: "2017-03-20T17: 17: 37.966Z" → "2017-03-20T17: 17: 37"

  • Find the first literal period. \.
  • → And after 4 characters .{4}

Replace the above group with an empty quote or nothing

SECOND -

for example: "2017-03-20T17: 17:37" → "2017-03-20 17:17:37"

  • Find the first instance of the character T

Replace the group above with a run or ""

This establishes that the string should be able to compare inside rethink db

NOTIFICATION. RethinkDB as a project has been dropped, but will be opened. If you need support, consider other resources for your projects.

+4
source

I think you misunderstood ReQL. It is assumed that the filter function runs on servers, not on your client.

You should change it to this:

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

r.now() returns the current time, suppose that upload_date is stored in RethinkDB's own time, you can subtract two time values, the result is how many seconds have passed. Then you can compare with 60 * 60 * 24 (how many seconds per day)

Written in style function will

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

This may confuse that the writing method looks like raw JavaScript. But this is not so. In fact, the client driver will evaluate the function on the client, build the request , in a JSON string, which RethinkDB understands and sends to the server. It does not work on the client to execute comparison logic. It generates a JSON string to tell RethinkDB what it wants using the API that is available at http://rethinkdb.com/api/javascript . Additional information about the driver: http://rethinkdb.com/docs/writing-drivers/

Secondly, if you upload_date not in your own RethinkDB object, you can try to convert it to the RethinkDB time type to easily manipulate some of these functions:

Or just try writing your time here and we will help you.

+4
source

Source: https://habr.com/ru/post/1232652/


All Articles