Couchbase中的条件向上插入
我有这样的文件:Couchbase中的条件向上插入,couchbase,Couchbase,我有这样的文件: { "name": "n", "age": 22 //other properties "hash": "XyRZHDJJD6738..." //This property contains the hash of the object (calculated by the client) } { "name": "n", "age": 22, "applied_hashes": { "XyRZHDJJD6738": null,
{
"name": "n",
"age": 22
//other properties
"hash": "XyRZHDJJD6738..." //This property contains the hash of the object (calculated by the client)
}
{
"name": "n",
"age": 22,
"applied_hashes": {
"XyRZHDJJD6738": null,
"AB2343DCxdsAd": null,
// ... other hashes
}
}
从客户处,我应了解是否:
- 仅当哈希值不同时,才使用其键更新文档(已知),(=>存储的对象和新对象不相同)
- 如果密钥不存在,请插入文档
在Couchbase(5.1+)中有这样做的方法吗?通过对文档模型的调整,您可以得到如下内容:
{
"name": "n",
"age": 22
//other properties
"hash": "XyRZHDJJD6738..." //This property contains the hash of the object (calculated by the client)
}
{
"name": "n",
"age": 22,
"applied_hashes": {
"XyRZHDJJD6738": null,
"AB2343DCxdsAd": null,
// ... other hashes
}
}
现在,您可以将每个更新作为子文档操作来执行,第一个规范是尝试将更新的哈希插入到应用的\u哈希中。如果以前应用过该散列/更新,则此插入将失败,并且由于子文档是原子文档,因此不会对文档进行任何更改
对于Java SDK 3.x,这看起来像:
try {
collection.mutateIn("id",
Arrays.asList(
MutateInSpec.insert("applied_hashes.XyRZHDJJD6738", null).createPath(),
MutateInSpec.upsert("age", 24)
// .. other parts of update XyRZHDJJD6738 here
));
}
catch (PathExistsException err) {
// Update XyRZHDJJD6738 has already been applied
// No changes have been made to the document
}
您的文档有多少个属性?它的结构复杂吗?(嵌套属性)最佳选项:给定键获取哈希,cas仅使用子文档API,然后如果不存在或不存在相同的哈希,则执行UPSERT。