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 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.
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
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"
\.
.{4}
Replace the above group them with empty quote, or nothing
SECOND -
eg: "2017-03-20T17:17:37" -> "2017-03-20 17:17:37"
T
Replace above group with a space character or " "
This sets the string to be able to compare inside rethink db
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With