Is number = number +1 n1ql atomic operation?

Hi,

I understand that there is an atomic operations (counter) to increase a number in couchbase.

I have learned that creating a separate document just for a counter makes my n1ql quite tricky when I join documents.

it looks like number = number + 1 works just fine.

update mybucket set number = number + 1 where name = “something”

Is it okay to rely on this query to update a number? Is this atomic?

It is atomic. Do you have concurrent updates of the same counter?

@geraldss

Thank you for confirming.

I’m not actually using a counter though.

I have a document

{
    "values": {
          "number": 0
     }
} 

Is this still atomic in this case?

And…yes, I might have concurrent update on the number field.

Yes, all updates are atomic per document. Internally, they use the CAS operation in Couchbase.

What happens if two concurrent connections try to update the number? As far as I understand, counter in couchbase does not fail as it is atomic. What about n1ql? Does it throw CAS exception?

I said already, N1QL uses CAS. Yes, CAS exception :slightly_smiling:

Thank you for confirming.

If it throws CAS exception, then it is not exactly same as how counter behaves. That is why I tried to double confirm. As far as I understand, counter api does not raise an exception when you try to update a number concurrently.

I think it is good enough though. Thank you!