Cosmos DB 补丁子对象

Cosmos DB Patch child object

我正在尝试使用新的部分文档更新功能修补 Cosmos Db 文档的子对象。但是,我找不到办法。

如果文档如下所示,您将如何使用新的 Patch 方法将 ID 为 2 ProductCode 属性 的子项(在 Items 数组中)更新为“A-111”?


{
    "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
}

您需要通过传递要更新的对象的特定索引来使用替换操作,

在这种情况下,它会是这样的,

 ItemResponse<SalesOrder> response = await container.PatchItemAsync<SalesOrder>(
 id: "SalesOrder2",
 partitionKey: new PartitionKey("/SalesOrder2"),
 patchOperations: new[] { PatchOperation.Replace("/Items/1/ProductCode","A-111") });