我想更新文档。我正在使用 couchbase 子文档 api 来更新文档。
在执行更新时,我手边只有文档 ID。但是,要获得当前的 Cas 值,我必须执行 get to the couchbase。
我的更新看起来像:
PrimaryBucket.mutateIn(document_id).upsert("path1","updatevalue1").upsert("path2","updatevalue2")
要处理乐观锁定,我想使用 "mutateIn(id).withCas(<currentcasvalue>)"
在执行更新时,我手边只有文档 ID。但是,要获得当前的 Cas 值,我必须执行 get to the couchbase。 有没有办法避免获取整个文档以仅获取 cas 值来执行更新。
这是正确的方法吗?
最佳答案
是的,有办法。您可以执行子文档查找以从文档中检索单个小路径,结果包括 CAS:
long cas = PrimaryBucket.lookupIn(document_id).get("path1").execute().cas();
PrimaryBucket.mutateIn(document_id).upsert("path1","updatevalue1").upsert("path2","updatevalue2").withCas(cas).execute();
如果您没有保证存在的路径,您可以使用 $document 虚拟 xattr 字段之一,如下所示:
long cas = PrimaryBucket.lookupIn(document_id).get("$document.exptime", new SubdocOptionsBuilder().xattr(true)).execute().cas();
关于couchbase - 如何在使用 couchbase 子文档 api 执行更新时获取 CAS 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57252109/