我正在尝试使用新的部分文档更新功能来修补 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==", …Run Code Online (Sandbox Code Playgroud) 尝试将新项目添加到父对象的子集合中。例如,在下面的代码中,如何将一个新项目添加到 Items 集合中?
{
"id": "SalesOrder2",
"ponumber": "PO15428132599",
"OrderDate": "2005-07-01T00:00:00",
"AccountNumber": "Account2",
"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
}
Run Code Online (Sandbox Code Playgroud)
做这样的事情:
public async Task AddProduct(Item item)
{
var response = await CosmosDbContainer.PatchItemAsync<Product>(
id: "a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711",
partitionKey:new PartitionKey("a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711"), …Run Code Online (Sandbox Code Playgroud)