Azure CosmosDB:函数应用

编程入门 行业动态 更新时间:2024-10-22 14:30:06
本文介绍了Azure CosmosDB:函数应用 - 如何更新文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 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:函数应用

本文发布于:2023-11-28 10:09:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Azure   CosmosDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!