0
votes

I am struggling with an azure function. The goal of the function is to get some objects from cosmos db, update them, and save them back.

The function looks more or less like this

[FunctionName("update-in-cosmosdb")]
public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
        [CosmosDB(
            databaseName: "name",
            collectionName: "col-name",
            ConnectionStringSetting = "COSMOSDB-CONNECTIONSTRING")] DocumentClient client,
        ILogger log)
    {
        var documentDatabase = Environment.GetEnvironmentVariable("DB-NAME");
        var documentCollection = Environment.GetEnvironmentVariable("COL-NAME");
        var uri = UriFactory.CreateDocumentCollectionUri(documentDatabase, documentCollection);
        var query = client.CreateDocumentQuery<MyModelClass>(uri,
            "SELECT TOP 10 c.id, c.PropertyToChange  FROM c WHERE c.id IN (" + ids+ ")", new FeedOptions {EnableCrossPartitionQuery = true}).ToList();

        foreach (var o in query)
        {
            o.PropertyToChange = someNewValue;
        }

then I would like to save update the corresponding documents in cosmos-db. What should be the way of doing this? What can be important is the fact, that MyModelClass contains only a few of the properties - the document in cosmos-db is way lot more complicated and I don't want to touch the other properties. MyModelClass of course contains Id

1
Use dynamic instead of Model classAgrawal Shraddha
.. or use JObject instead of Model class.Imre Pühvel

1 Answers

2
votes

You are looking for Partial update, Currently there is no way rather than retrieving the document doing the modification and updating it.

You need to store the entire document as a new one with the changes if you want to update it.