2

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).

Sagivf
  • 705
  • 6
  • 10

2 Answers2

3

You can also write .update({field_a: 'value_a', field_b: r.literal()}) to change one field and remove another field at the same time.

mlucy
  • 5,249
  • 1
  • 17
  • 21
2
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

  • 2
    Thanks @Neil, works like a chame! I think you can do it without the anonymous function as well. `r.row.merge({field_a: 'newval'}).without('field_b')` – Sagivf Apr 12 '16 at 15:52