3

I am using rethinkdbdash in Node js connecting to the rethinkdb. Using the below code I get the whole document that has been changed (old and new values).

r.db(db).table('test')
  .changes()
  .run()
  .then(function(feed){
    feed.each(function(err, item){
      io.emit('change message', item);
    })
  })

I only want the field that has been changed and the new value.

Any idea how to solve this issue?

thanks in advance!!

Big Skinny
  • 53
  • 7

1 Answers1

2

You can transform the change documents on the server to get them into the form you want. Something like this might do what you want:

r.table('test').changes().map(function(row) {
  return row('old_val').keys().setUnion(row('new_val').keys()).concatMap(function(key) {
    return r.branch(row('old_val')(key).ne(row('new_val')(key)).default(true),
                    [[key, row('new_val')(key).default(null)]],
                    []);
  }).coerceTo('object');
})
mlucy
  • 5,249
  • 1
  • 17
  • 21
  • 1
    Hi, thank you for your reply. If I run this, it will pass the object twice. Don't know why. It is also possible to pass the id of the document? – Big Skinny Nov 28 '15 at 15:03
  • You can always pass the `id` by changing `row('old_val')(key).ne(row('new_val')(key))` to `key.eq('id').or(row('old_val')(key).ne(row('new_val')(key)))` – mlucy Nov 28 '15 at 22:30
  • Is it possible to return another column property besides id? I am trying to add another column called 'name' next to the column 'id' in the change fee, but can't figure it out. – Big Skinny Mar 02 '16 at 23:14