Cosmos DB 补丁子对象
Posted
技术标签:
【中文标题】Cosmos DB 补丁子对象【英文标题】:Cosmos DB Patch child object 【发布时间】:2022-01-02 21:33:35 【问题描述】:我正在尝试使用新的部分文档更新功能修补 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
【问题讨论】:
您使用的是 Dotnet 还是 Java 还是 Nodejs? 我正在使用 Dotnet 【参考方案1】:您需要通过传递要更新的对象的特定索引来使用替换操作,
在这种情况下,它会是这样的,
ItemResponse<SalesOrder> response = await container.PatchItemAsync<SalesOrder>(
id: "SalesOrder2",
partitionKey: new PartitionKey("/SalesOrder2"),
patchOperations: new[] PatchOperation.Replace("/Items/1/ProductCode","A-111") );
【讨论】:
以上是关于Cosmos DB 补丁子对象的主要内容,如果未能解决你的问题,请参考以下文章
Cosmos DB - ExecuteNextAsync 返回空对象
带有 Cosmos DB 绑定的 Azure 函数返回格式化的 DateTime?
Azure Cosmos SQL API - 如何将自定义对象存储为 @PartitionKey
无法使用 Microsoft.EntityFrameworkCore.Cosmos 连接到 Azure Cosmos Db 帐户 - 响应状态代码