Atomic operations Rethinkdb

Say I have a document

{
    id: 1,
    fruits: []
}

The fruits here act like SET

Now I want to atomically add a value for a fruit array for a document with primary key = 1 OR create such a document if it does not exist (for example, use SetInsert ReQL under the hood)

I also need to do the same for increment (ReQL.Add)

Obviously this cannot be done in client code, as it breaks atomicity, and I end up with inconsistent data

I wish something like this were possible

r.table('results').insert({
  id: '62c70132-6516-4279-9803-8daf407ffa5c',
  counter: r.row('counter').add(1).default(0)
}, {conflict: "update"})

but it dies with "RqlCompileError: r.row is not defined in this context in"

Any help / guidance is appreciated, thanks!

+4
source share
2 answers

insert. , . https://github.com/rethinkdb/rethinkdb/issues/3753.

replace upsert:

r.table('results').get('62c70132-6516-4279-9803-8daf407ffa5c')
 .replace({
  id: '62c70132-6516-4279-9803-8daf407ffa5c',
  counter: r.row('counter').add(1).default(0)
})

replace , .

+6

RethinkDB . Rethink , - / , . , , . update replace. insert.

, , , , .

, , insert , replace:

r.table('FruitStore')
.get(1)
.replace({
  id : 1, 
  fruits : r.row('fruits').default([]).append('orange') 
})

... . , :

r.table('FruitStore')
.get(1)
.replace({
  id : 1, 
  count : r.row('count').default(0).add(1) 
})
+2
source

Source: https://habr.com/ru/post/1599707/


All Articles