|
| 1 | +--- |
| 2 | +title: Azure CosmosDB for MongoDB (vCore) Input Binding for Azure Functions |
| 3 | +description: Understand how to use Azure Cosmos DB for MongoDB (vCore) input binding to read items from the database. |
| 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) input binding for Azure Functions |
| 13 | + |
| 14 | +This article explains how to work with the [Azure Cosmos DB for MongoDB vCore](/azure/cosmos-db/mongodb/vcore/introduction) input binding in Azure Functions. |
| 15 | + |
| 16 | +The Azure Cosmos DB for MongoDB (vCore) input binding lets you retretieve one or more items as documents from the database. |
| 17 | + |
| 18 | +[!INCLUDE [functions-bindings-mongodb-vcore-preview](../../includes/functions-bindings-mongodb-vcore-preview.md)] |
| 19 | + |
| 20 | +## Example |
| 21 | + |
| 22 | +This example shows a Timer trigger function that uses an input binding to execute a periodic query against the database: |
| 23 | + |
| 24 | +```csharp |
| 25 | +using Microsoft.Azure.WebJobs; |
| 26 | +using Microsoft.Azure.WebJobs.Extensions.AzureCosmosDb.Mongo; |
| 27 | +using Microsoft.Extensions.Logging; |
| 28 | +using MongoDB.Bson; |
| 29 | +using MongoDB.Driver; |
| 30 | + |
| 31 | +namespace Sample |
| 32 | +{ |
| 33 | + public static class Sample |
| 34 | + { |
| 35 | + [FunctionName("InputBindingSample")] |
| 36 | + public static async Task InputBindingRun( |
| 37 | + [TimerTrigger("*/5 * * * * *")] TimerInfo myTimer, |
| 38 | + [CosmosDBMongo("%vCoreDatabaseTrigger%", "%vCoreCollectionTrigger%", ConnectionStringSetting = "vCoreConnectionStringTrigger", |
| 39 | + QueryString = "%queryString%")] List<BsonDocument> docs, |
| 40 | + ILogger log) |
| 41 | + { |
| 42 | + log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); |
| 43 | + |
| 44 | + foreach (var doc in docs) |
| 45 | + { |
| 46 | + log.LogInformation(doc.ToString()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +The examples refer to a simple `TestClass` type: |
| 55 | + |
| 56 | +```cs |
| 57 | +namespace Sample |
| 58 | +{ |
| 59 | + public class TestClass |
| 60 | + { |
| 61 | + public string id { get; set; } |
| 62 | + public string SomeData { get; set; } |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Attributes |
| 68 | + |
| 69 | +This table describes the binding configuration properties of the `CosmosDBMongoTrigger` attribute. |
| 70 | + |
| 71 | +|Parameter | Description| |
| 72 | +|---------|----------------------| |
| 73 | +|**FunctionId** | (Optional) The ID of the trigger function. | |
| 74 | +|**DatabaseName** | The name of the database being monitored by the trigger for changes. | |
| 75 | +|**CollectionName** | The name of the collection in the database being monitored by the trigger for changes.| |
| 76 | +|**ConnectionStringSetting** | The name of an app setting or setting collection that specifies how to connect to the Azure Cosmos DB account being monitored. | |
| 77 | +|**QueryString** | Defines the Mongo query expression used by the input binding return documents from the database. The query supports binding parameters. | |
| 78 | + |
| 79 | +## Usage |
| 80 | + |
| 81 | +You can use the `CosmosDBMongo` attribute to obtain and work directly with the [MongoDB client](https://mongodb.github.io/mongo-csharp-driver/2.8/apidocs/html/T_MongoDB_Driver_IMongoClient.htm) in your function code: |
| 82 | + |
| 83 | +:::code language="csharp" source="~/azure-functions-mongodb-extension/Sample/Sample.cs" range="17-29" ::: |
| 84 | + |
| 85 | +## Related articles |
| 86 | + |
| 87 | +- [Azure Cosmos DB for MongoDB (vCore)](/azure/cosmos-db/mongodb/vcore/introduction.md) |
| 88 | +- [Azure Cosmos DB for MongoDB (vCore) bindings for Azure Functions](functions-bindings-mongodb-vcore.md) |
| 89 | +- [Azure Cosmos DB for MongoDB (vCore) trigger for Azure Functions](functions-bindings-mongodb-vcore-trigger.md) |
| 90 | +- [Azure Cosmos DB for MongoDB(vCore) output binding for Azure Functions](functions-bindings-mongodb-vcore-output.md) |
0 commit comments