Azure CosmosDB:函数应用 - 如何更新文档 [英] Azure CosmosDB: function app- how to update a document

查看:20
本文介绍了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:

https://feedback.azure.com/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=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

这篇关于Azure CosmosDB:函数应用 - 如何更新文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆