gmadar
gmadar

Reputation: 1904

Upsert & increment in RethinkDB

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

Upvotes: 1

Views: 780

Answers (1)

Kludge
Kludge

Reputation: 2835

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

Upvotes: 1

Related Questions