Update value using cas value inside query

hello again couchbasia

was wonder where exactly can i specify the CAS value inside an UPDATE statement.(or any other in query statement)
also, is it possible to set the entire document instead of a signle property name? (SET proeprtyName = …, propertyName2 = …)

thanks in advance

@mazorsk ,

You will not able supply cas value to N1QL.

UPDATE default AS d
SET d.proeprtyName = “xyz”
WHERE …;

Based on predicate qualifying documents N1QL fetch the documents from KV and apply predicate and mutate the document and writes back.
During fetch it will remember the document cas and during write it supplies the cas (all internal) .
If concurrent user modified and on cas miss match it returns error to user (Unless you use transactions some documents might have updated some may fail of cas miss match) . User must retry the operation.

To Set entire document use as follows.

UPDATE default AS d
SET d = …
WHERE …;

upsert into default values ("k01",{"a":1});
UPDATE default AS d SET d = {"b":20, "a":1}  WHERE d.a = 1;
UPDATE default AS d  USE KEYS "k01" SET  d = {"b":20, "a":1} ;

If you are going to update whole document means, it is single document update.
In that case you can use SDK directly to update if you know the key. If you don’t know the key , you can use query get the document keys only and use SDK Update the document also.

If you are not interested old values not required check concurrent modifications you can also explore UPSERT (over write)

1 Like