Reputation: 31
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?
Upvotes: 3
Views: 1166
Reputation: 516
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();
Upvotes: 1