Let's say I have a document
{
id: 1,
fruits: []
}
fruits here acts as a SET
Now I want to atomically add a value to fruits array for document with primary key = 1 OR create such document if it does not exist(i.e. use SetInsert ReQL under the hood)
I also need to do the same for increment(ReQL .Add)
Obviously this can't be done in client code as it breaks atomicity and I end up with inconsistent data
I wish something like this was 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!
r.row
basically means "the current row for each row you will operate on" in ReQL-speak. In an insert,r.row
doesn't make sense which is why that error occurs. However, if you're looking for a particular document then there is no reason you can't get the document's field with a subquery, see my answer below on that :) – Launalaunce