我是 Azure 的新手.我想知道是否可以通过 https 触发器更新现有记录.
I am new to Azure. I was wondering if I could get some help with updating an existing record via https trigger.
我在网上找到的许多解决方案要么是创建新记录,要么是更新完整的文档.我只想更新文档中的 2 个属性.
Many solutions I find online are either creating a new record or updating complete document. I just want to update 2 properties in the document.
I tried [this][1] and the following code but it didn't work [FunctionName("Function1")] public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, [DocumentDB("MyDb", "MyCollection", ConnectionStringSetting = "MyCosmosConnectionString")] out dynamic document, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); dynamic data = req.Content.ReadAsAsync<object>().GetAwaiter().GetResult(); document = data; return req.CreateResponse(HttpStatusCode.OK); }我想传递文档可以根据主字符串更新的主键和 2 个其他值.有人可以帮忙吗?
I want to pass primary key and 2 other values which the document can update based on the primary string. Can anyone help?
推荐答案我只想更新文档中的 2 个属性.
I just want to update 2 properties in the document.
截至 2020/10/20,此功能仍不支持.您可以在此查看进度:
Till 2020/10/20, this feature still not support. You can check the progress rate in this place:
feedback.azure/forums/263030-azure-cosmos-db/suggestions/6693091-be-able-to-do-partial-updates-on-document#{toggle_previous_statuses}
支持该功能的工作一年前就开始了,现在还没有完成,我们唯一能做的就是等待.
The work to support the feature start one year ago, and now is still not finished, the only thing we can do is wait.
在你这边,你需要获取文档,更改内部然后更新.
On your side, you need to get the document, change the internal and then update.
一个简单的例子:
using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Azure.Cosmos; using System.Collections.Generic; namespace FunctionApp21 { public static class Function1 { private static CosmosClient cosmosclient = new CosmosClient("AccountEndpoint=testbowman.documents.azure:443/;AccountKey=xxxxxx;"); [FunctionName("Function1")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { CosmosContainer container = cosmosclient.GetContainer("testbowman", "testbowman"); ItemResponse<ToDoActivity> wakefieldFamilyResponse = await container.ReadItemAsync<ToDoActivity>("testbowman", new PartitionKey("testbowman")); ToDoActivity itemBody = wakefieldFamilyResponse; itemBody.status = "This is been changed."; wakefieldFamilyResponse = await container.ReplaceItemAsync<ToDoActivity>(itemBody, itemBody.id, new PartitionKey(itemBody.testbowman)); return new OkObjectResult(""); } } public class ToDoActivity { public string id { get; set; } public string status { get; set; } public string testbowman { get; set; } } }官方文档:
docs.microsoft/en-us/azure/cosmos-db/create-sql-api-dotnet-v4#replace-an-item
更多推荐
Azure CosmosDB:函数应用
发布评论