mk-conn
mk-conn

Reputation: 11

RethinkDB listen for changes on filter query?

I'm not sure if I fully understand the changefeeds correct... What I need to do is filter data based on current date like this:

r.table("messages")
  .filter(
    r.row("start").lt(r.now().toISO8601())
      .and(r.row("end").gt(r.now().toISO8601()))
  )
  .changes()
  .run(conn, (err, cursor) => {
    if (err) {
      throw err;
    }

    cursor.each(function (err, row) {
      if (err) {
        throw err;
      }

     // do stuff here

    });
  });

});

Is it even possible to do something like this? Or will .changes() only work if a field of a row is updated?

Upvotes: 1

Views: 495

Answers (1)

kureikain
kureikain

Reputation: 2314

It's possible and syntax is right.

However, just a note in case you miss it. The r.now() evaluated to the first time you ever run query, not the current time when changes happens. r.now() is calculated only one time in same query and the return value is re-use on sub sequent calls: https://www.rethinkdb.com/api/javascript/now/

Changefeeds works with both of insert and updating. If the insert/updated document match the condition of your filter, it will be return in cursor. If it's an insert, old_val is null, if it's an update, old_val is old value.

Upvotes: 1

Related Questions