Search code examples
rethinkdbrethinkdb-javascript

RethinkDB update and replace-without in one line


Can anyone think if an elegant way to do update and replace in one line?

I want to use the r.row.without to remove fields and update at the same query.

Something like:

r.db('db').table('table').get('item_id')
          .update({ field_a:'value_a'})
          .replace(r.row.without(r.args(['field_b'])))`

simply chaining would be nice but this won't work (update returns a change result).


Solution

  • r.db('db').table('table').get('item_id')
        .replace(
            r.row.merge(
                function(doc){ 
                    return {field_a: 'newval'}
                }
            ).without('field_b')
        )
    

    Should do the trick