|
| 1 | +--- |
| 2 | +title: Azure CosmosDB for MongoDB(vCore) Trigger for Azure Functions |
| 3 | +description: Understand how to use Azure Cosmos DB for MongoDB(vCore) triggers and bindings in Azure Functions. |
| 4 | +author: sajeetharan |
| 5 | +ms.author: sasinnat |
| 6 | +ms.topic: reference |
| 7 | +ms.date: 5/8/2025 |
| 8 | +ms.custom: |
| 9 | + - build-2025 |
| 10 | +--- |
| 11 | + |
| 12 | +# Azure Cosmos DB for MongoDB(vCore) bindings for Azure Functions |
| 13 | + |
| 14 | +This article explains how to work with [Azure Cosmos DB for MongoDB vCore](/azure/cosmos-db/mongodb/vcore/introduction) bindings in Azure Functions. Azure Functions supports trigger, input, and output bindings for Azure Cosmos DB for MongoDB(vCore) |
| 15 | + |
| 16 | + |
| 17 | +* The Azure Cosmos DB for MongoDB(vCore) bindings for the Functions runtime don't support Microsoft Entra authentication and managed identities. To improve security, you should upgrade to run on the latest version of the Functions runtime. |
| 18 | + |
| 19 | +## Attributes |
| 20 | + |
| 21 | +The following table explains the binding configuration properties that you set in the function.json file and the CosmosDBMongoTrigger attribute. |
| 22 | + |
| 23 | +|Parameter | Description| |
| 24 | +|---------|----------------------| |
| 25 | +|**FunctionId** |Optional, the Id of trigger | |
| 26 | +|**DatabaseName** | The name of the database to which the parameter applies| |
| 27 | +|**CollectionName** | The name of the database to which the parameter applies| |
| 28 | +|**ConnectionStringSetting** | The name of the database to which the parameter applies| |
| 29 | +|**TriggerLevel** | The name of the database to which the parameter applies| |
| 30 | + |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +### Output |
| 35 | + |
| 36 | +The Azure Cosmos DB for MongoDB(vCore) output binding lets you write a new document to an Azure Cosmos DB for MongoDB(vCore) |
| 37 | + |
| 38 | +### Example |
| 39 | + |
| 40 | +The examples refer to a simple `TestClass` type: |
| 41 | + |
| 42 | +```cs |
| 43 | +namespace Sample |
| 44 | +{ |
| 45 | + public class TestClass |
| 46 | + { |
| 47 | + public string id { get; set; } |
| 48 | + public string SomeData { get; set; } |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | +### TimerTrigger trigger |
| 53 | + |
| 54 | +```cs |
| 55 | +namespace Sample |
| 56 | +{ |
| 57 | + public class TestClass |
| 58 | + { |
| 59 | + public string id { get; set; } |
| 60 | + public string SomeData { get; set; } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +```cs |
| 66 | +using Microsoft.Azure.WebJobs; |
| 67 | +using Microsoft.Azure.WebJobs.Extensions.AzureCosmosDb.Mongo; |
| 68 | +using Microsoft.Extensions.Logging; |
| 69 | +using MongoDB.Bson; |
| 70 | +using MongoDB.Driver; |
| 71 | + |
| 72 | +namespace Sample |
| 73 | +{ |
| 74 | + public static class Sample |
| 75 | + { |
| 76 | + [FunctionName("OutputBindingSample")] |
| 77 | + public static async Task OutputBindingRun( |
| 78 | + [TimerTrigger("*/5 * * * * *")] TimerInfo myTimer, |
| 79 | + [CosmosDBMongo("%vCoreDatabaseBinding%", "%vCoreCollectionBinding%", ConnectionStringSetting = "vCoreConnectionStringBinding")] IAsyncCollector<TestClass> CosmosDBMongoCollector, |
| 80 | + ILogger log) |
| 81 | + { |
| 82 | + log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); |
| 83 | + |
| 84 | + TestClass item = new TestClass() |
| 85 | + { |
| 86 | + id = Guid.NewGuid().ToString(), |
| 87 | + SomeData = "some random data" |
| 88 | + }; |
| 89 | + await CosmosDBMongoCollector.AddAsync(item); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +``` |
| 95 | +## #Input |
| 96 | + |
| 97 | +The Azure Cosmos DB for MongoDB(vCore) input binding lets you reretieve one or more Azure CosmosDB for MongoDB(vCore) documents. |
| 98 | + |
| 99 | +### Example |
| 100 | + |
| 101 | +The examples refer to a simple `TestClass` type: |
| 102 | + |
| 103 | +```cs |
| 104 | +namespace Sample |
| 105 | +{ |
| 106 | + public class TestClass |
| 107 | + { |
| 108 | + public string id { get; set; } |
| 109 | + public string SomeData { get; set; } |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | +### TimerTrigger trigger |
| 114 | + |
| 115 | +```cs |
| 116 | +namespace Sample |
| 117 | +{ |
| 118 | + public class TestClass |
| 119 | + { |
| 120 | + public string id { get; set; } |
| 121 | + public string SomeData { get; set; } |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +```cs |
| 127 | +using Microsoft.Azure.WebJobs; |
| 128 | +using Microsoft.Azure.WebJobs.Extensions.AzureCosmosDb.Mongo; |
| 129 | +using Microsoft.Extensions.Logging; |
| 130 | +using MongoDB.Bson; |
| 131 | +using MongoDB.Driver; |
| 132 | + |
| 133 | +namespace Sample |
| 134 | +{ |
| 135 | + public static class Sample |
| 136 | + { |
| 137 | + [FunctionName("InputBindingSample")] |
| 138 | + public static async Task InputBindingRun( |
| 139 | + [TimerTrigger("*/5 * * * * *")] TimerInfo myTimer, |
| 140 | + [CosmosDBMongo("%vCoreDatabaseTrigger%", "%vCoreCollectionTrigger%", ConnectionStringSetting = "vCoreConnectionStringTrigger", |
| 141 | + QueryString = "%queryString%")] List<BsonDocument> docs, |
| 142 | + ILogger log) |
| 143 | + { |
| 144 | + log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); |
| 145 | + |
| 146 | + foreach (var doc in docs) |
| 147 | + { |
| 148 | + log.LogInformation(doc.ToString()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +``` |
| 156 | + |
| 157 | +## Related articles |
| 158 | + |
| 159 | + - [Azure Cosmos DB for MongoDB(vCore)](/azure/cosmos-db/mongodb/vcore/introduction.md) |
| 160 | + - [Azure Azure Cosmos DB for MongoDB(vCore) bindings for Azure Functions](/azure/azure-functions/functions-bindings-overview-mongodb-vore-csharp-only) |
0 commit comments