Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CAS value while performing an update using couchbase subdocument api?

I want to perform update on documents. I am using couchbase subdocument api to update the documents.

While performing the update,I only have the document id at hand. However to get the current Cas value I have to perform a get to the couchbase.

My update looks like:

PrimaryBucket.mutateIn(document_id).upsert("path1","updatevalue1").upsert("path2","updatevalue2")

To handle optimistic locking, i want to use "mutateIn(id).withCas(<currentcasvalue>)"

While performing the update,I only have the document id at hand. However to get the current Cas value I have to perform a get to the couchbase. Is there a way to avoid fetching the whole document in order to only get the cas value to perform the update.

Is this the correct approach?

like image 852
Reetika Ojha Avatar asked Dec 05 '25 08:12

Reetika Ojha


1 Answers

Yes, there is a way. You can do a Sub-Document lookup to retrieve a single small path from the document, and the result includes the CAS:

long cas = PrimaryBucket.lookupIn(document_id).get("path1").execute().cas();

PrimaryBucket.mutateIn(document_id).upsert("path1","updatevalue1").upsert("path2","updatevalue2").withCas(cas).execute();

If you don't have a path that's guaranteed to exist you can use one of the $document virtual xattr fields like so:

long cas = PrimaryBucket.lookupIn(document_id).get("$document.exptime", new SubdocOptionsBuilder().xattr(true)).execute().cas();
like image 123
Graham Pople Avatar answered Dec 06 '25 20:12

Graham Pople