Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Couchbase中的条件向上插入_Couchbase - Fatal编程技术网

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。