我是Azure的新手。我想知道是否可以通过https触发器获取有关更新现有记录的帮助。
我在网上找到许多解决方案,要么创建新记录,要么更新完整文档。我只想更新文档中的2个属性。
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个其他值,文档可以基于主字符串进行更新。有人可以帮忙吗?
答案 0 :(得分:2)
我只想更新文档中的2个属性。
到2020/10/20,此功能仍不支持。您可以在这里查看进度:
支持该功能的工作始于一年前,现在仍未完成,我们唯一能做的就是等待。
您需要获取文档,更改内部文档,然后更新。
一个简单的例子:
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=https://testbowman.documents.azure.com: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; }
}
}
官方文档:
https://docs.microsoft.com/en-us/azure/cosmos-db/create-sql-api-dotnet-v4#replace-an-item