public static class ctTestCosmos
    {
        [FunctionName("ctTestCosmos")]
        public static void Run([CosmosDBTrigger(
            databaseName: "MyCosmosDB",
            collectionName: "MyOrders",
            ConnectionStringSetting = "MyCosmosDBConnection",
            LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists =true)]IReadOnlyList<Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                log.LogInformation("Documents modified " + input.Count);
                log.LogInformation("First document Id " + input[0].Id);
            }
        }
    }

Get specified field value…

foreach (var item in input)
{
     log.LogInformation("Order Id " + item.GetPropertyValue<int>("OrderId"));
}

Note:

  • Make sure you installed the Microsoft.Azure.WebJobs.Extensions.CosmosDB package.
  • Created a CosmosDB connection string (Get string value from Azure Portal).
  • Created a container called “leases” or set CreateLeaseCollectionIfNotExists =true.
Last modified: August 5, 2021

Author

Comments

Write a Reply or Comment