最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

RethinkDB JavaScript Date Filter - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question edited Sep 30, 2015 at 19:44 Eric Hodonsky asked Sep 30, 2015 at 19:22 Eric HodonskyEric Hodonsky 5,9074 gold badges28 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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 pare 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 paring. It generates a JSON string to tell RethinkDB what it wants using the API that is available on http://rethinkdb./api/javascript. More information about how driver works: http://rethinkdb./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./api/javascript/iso8601/
  • epoch_time: http://rethinkdb./api/javascript/epoch_time/

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

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. )

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 pare 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.

发布评论

评论列表(0)

  1. 暂无评论