1

I am new to Azure. I was wondering if I could get some help with updating an existing record via https trigger.

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);
    }

I want to pass primary key and 2 other values which the document can update based on the primary string. Can anyone help?

TheDeveloper
  • 1,127
  • 1
  • 18
  • 55
  • What is the error you are getting when it doesn't work? Cosmos DB Upsert requires the full document (not just the properties you want to update), it's a full document replace. – Matias Quaranta Oct 19 '20 at 23:31
  • NO error, just not updating. So should i make two calls? one get doc and then update it ? – TheDeveloper Oct 19 '20 at 23:42
  • 1
    Upsert takes the full document payload. If a document with the id and Partition Key value matches, it will replace the existing document with your payload. If it doesn't exist, it will create it. – Matias Quaranta Oct 20 '20 at 00:01
  • @MatiasQuaranta do you have any example you can share ? – TheDeveloper Oct 20 '20 at 01:09
  • are you suggesting to use 2 function apps? 1 to pull document and then update it and use another function app to upsert it ? – TheDeveloper Oct 20 '20 at 01:30

1 Answers1

2

I just want to update 2 properties in the document.

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.

A simple example:

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; }
    }
}

The offcial doc:

https://learn.microsoft.com/en-us/azure/cosmos-db/create-sql-api-dotnet-v4#replace-an-item

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27