Azure CosmosDB - 复杂结构的部分文档更新(补丁)

Azure CosmosDB - Partial Document Update (Patch) on Complex Structures

我正在尝试使用部分文档更新(补丁 API)来更新文档中的子对象,但我 运行 遇到了麻烦。我发现 这与我遇到的问题相同。但是,接受的答案通过按索引引用数组中的对象来解决问题。我不相信我有能力做到这一点。因此,要使用与另一个问题相同的示例文档...

{
    "id": "SalesOrder2",
    "ponumber": "PO15428132599",
    "OrderDate": "2005-07-01T00:00:00",
    "DueDate": "2005-07-13T00:00:00",
    "ShippedDate": "2005-07-08T00:00:00",
    "AccountNumber": "Account2",
    "SubTotal": 6107.082,
    "TaxAmt": 586.1203,
    "Freight": 183.1626,
    "TotalDue": 4893.3929,
    "DiscountAmt": 1982.872,
    "Items": [
        {
            "Id": 1,
            "OrderQty": 3,
            "ProductCode": "A-123",
            "ProductName": "Product 1",
            "CurrencySymbol": "$",
            "CurrencyCode": "USD",
            "UnitPrice": 17.1,
            "LineTotal": 5.7
        },
        {
            "Id": 2,
            "OrderQty": 2,
            "ProductCode": "A-456",
            "ProductName": "Product 2",
            "CurrencySymbol": "$",
            "CurrencyCode": "USD",
            "UnitPrice": 10,
            "LineTotal": 20
        }
    ],    
    "_rid": "BsMkAMc43s4CAAAAAAAAAA==",
    "_self": "dbs/BsMkAA==/colls/BsMkAMc43s4=/docs/BsMkAMc43s4CAAAAAAAAAA==/",
    "_etag": "\"00000000-0000-0000-e136-0dbec04601d7\"",
    "_attachments": "attachments/",
    "_ts": 1637760030
}

我无法保证 Items 数组中 ID 为 1 的项目位于数组的位置 0。同样,Id 为 2 的项目不能保证在位置 1。因此我认为我需要使用 Patch API 的 FilterPredicate 参数来过滤我的结果。但是当我尝试这样做时,我不断收到以下异常:

Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: PreconditionFailed (412); Substatus: 1110; ActivityId: dbd258ae-0a0a-4a9b-8c25-1d36e137b7c5; Reason: ();

如果您能就如何完成此任务提供任何帮助,我们将不胜感激。

正如我在附件link中的回答,Patch需要用户传递需要更新的对象的具体索引。我们正在努力在未来几个月启用此特定功能,但是作为替代方案,您应该查看 Conditional Patch

代码将是这样的,

response = patch(operation, Condition(check if item exists))

if(response == fail/precondition failed)
{
PatchOperation operation = PatchOperation.Add("/Items", [{"Id" : "P-1", "Description" : "My Product"}]);
}