1

Is it possible to do an "upsert" with an increment? So if the query is an insert the counter will initialize with 1 and if it an update it will increment the counter by 1

gmadar
  • 1,884
  • 3
  • 19
  • 22

1 Answers1

1
table.insert({id: 1, counter: 1}, {conflict: function(id, oldVal, newVal) {
  return newVal.merge({counter: oldVal('counter').add(1)})
}})

Introduced in v2.3.0 IIUC: https://github.com/rethinkdb/rethinkdb/releases/tag/v2.3.0

Kludge
  • 2,653
  • 4
  • 20
  • 42
  • Apologies, I'm not a rethinkdb guru, but wouldn't this require `counter` to be unique? I thought RethinkDB only supported uniqueness on the primary key (which in this example I would assume is `id`) – Endophage Aug 10 '17 at 21:23