Search code examples
rethinkdbrethinkdb-javascript

Rethinkdb query for data between 2 dates


I would like to run a query that gets all the documents that have a lastUpdateDate from a date provided until today.

lastUpdateDated is defined like

lastUpdateDate = new Date() -> Tue Jan 19 2016 20:45:32 GMT+00:00

The following works in the RethinkDB Admin console

r.db('water').table('ground_water').filter(function (test) {
  return test("lastUpdateDate").during(r.time(2015,1,1, 'Z'), r.now().date())
});

But here is the actual code (I have to do some processing on the date)

.table('ground_support_water_tests')
              .filter(function(test) {
                return test("lastUpdateDate").during(
                  r.time(2016,1,19, 'Z'),
                  r.now().date())
              })
              .run()
              .then((results) => {
                console.log(results);
                done(null, results);
              })
              .catch((err) => {console.log(err); });

This returns no errors or results. I obviously like to not hardcode the date there so I have some logic to make a new r.time(yyyy,dd,mm) but that gives me the same results as this hardcoded one.


Solution

  • I think your query may contains some pitfalls.

    First, I suggest you add rightBound: "closed" to option. Because you are comparing on date() and you don't care about time at all.

    Second, I suggest you to change test("lastUpdateDate") -> test("lastUpdateDate").date() because you're removing time with date and it become Wed Jan 20 2016 00:00:00 GMT+00:00 while as your test("lastUpdateDate") is Wed Jan 20 2016 18:00:00 GMT+00:00 for example.

    So let's try this:

    .table('ground_support_water_tests')
                  .filter(function(test) {
                    return test("lastUpdateDate").date().during(
                      r.time(2016,1,19, 'Z'),
                      r.now().date())
                  }, {rightBound: "closed"})
                  .run()
                  .then((results) => {
                    console.log(results);
                    done(null, results);
                  })
                  .catch((err) => {console.log(err); });
    

    Update:

    I tried using NodeJS with official drive:

    var r = require('rethinkdb')
    
    r.connect().then(function(conn) {
      r.table('t')
      .filter((test) => {
          return test("lastUpdateDate").date().during(r.time(2015,1,1, 'Z'), r.now().date(), {rightBound: "closed"})
      })
      .run(conn)
      .then((cursor) => { return cursor.toArray() })
      .then((data) => { console.log(data) })
    })
    

    On this date set:

    [{
    "id":  "4917c8a1-1639-400c-964c-458d58b5bfcc" ,
    "lastUpdateDate": Wed Jan 20 2016 21:12:51 GMT+00:00
    }]
    

    The query returns properly data.