Friday, 15 June 2012

How to make a rethinkdb atomic update if document exists, insert otherwise? -



How to make a rethinkdb atomic update if document exists, insert otherwise? -

how create rethinkdb atomic update if document exists, insert otherwise?

i want like:

class="lang-js prettyprint-override">var tab = r.db('agflow').table('test'); r.expr([{id: 1, n: 0, x: 11}, {id: 2, n: 0, x: 12}]).foreach(function(row){ var _id = row('id'); homecoming r.branch( tab.get(_id).eq(null), // 1 tab.insert(row), // 2 tab.get(_id).update(function(row2){return {n: row2('n').add(row('n'))}}) // 3 )})

however not atomic, because between time when check if document exists (1) , inserting (2) other thread may insert it.

how create query atomic?

i think solution passing

conflict="update"

to insert method.

als see rethinkdb documentation on insert

rethinkdb

No comments:

Post a Comment