MutateArrayAppend when document may be missing

I need to update (append) an array in a document which in some situations is not created yet. If Is missing should create it first before updating. Is there a way to do this in a single operation?

Now I am doing something like this:

  1. check if document exists
  2. if document exists → update
  3. if document doesn’t exist → create

The problem I have is that in a situation with high concurrency between the moment in which I check if the document exists and update or insert, the status of the document may change (like the document could be created by another worker, or could be deleted by another worker)

What is the best approach to do this?
btw, I am using PHP

If you’re executing SQL++ via the SDK, then you could make use of the MERGE statement:
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/merge.html

HTH.

This topic was automatically closed after 90 days. New replies are no longer allowed.