Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
The Azure DocumentDB extension is currently in preview. At this time, only .NET apps that use the legacy in-process model are supported.
The Azure DocumentDB trigger monitors change streams for inserts and updates in your DocumentDB collections. This article explains how to configure and use the trigger in Azure Functions, including code examples and attribute parameters to help you respond to data changes in real-time.
The feed publishes only new and updated items. Watching for delete operations using change streams is currently not supported.
Prerequisites
An Azure subscription
- If you don't have an Azure subscription, create a free account
An existing Azure DocumentDB cluster
If you don't have a cluster, create a new cluster
Change stream feature enabled
Azure Functions .NET 8.0 project using the legacy in-process worker model
Microsoft.Azure.WebJobs.Extensions.AzureCosmosDb.MongoNuGet package
Examples
This example shows a function triggered by an insert or replacement operation on a collection in Azure DocumentDB. The function logs the body of the changed document.
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.AzureCosmosDb.Mongo;
using MongoDB.Bson;
using MongoDB.Driver;
public static class DocumentDBTrigger
{
[FunctionName(nameof(DocumentDBTrigger))]
public static void Run(
[CosmosDBMongoTrigger(databaseName: "<database-name>",
collectionName: "<collection-name>",
ConnectionStringSetting = "AZURE_DOCUMENTDB_CONNECTION_STRING")]
ChangeStreamDocument<BsonDocument> change,
ILogger logger)
{
logger.LogInformation("C# Azure DocumentDB trigger function processed a change.");
logger.LogInformation($"{change.FullDocument}");
}
}
Attributes
This table describes the binding configuration properties of the CosmosDBMongoTrigger attribute.
| Parameter | Description |
|---|---|
FunctionId |
(Optional) The ID of the trigger function. |
DatabaseName |
The name of the database monitored by the trigger for changes. A valid value is required unless TriggerLevel is set to MonitorLevel.Cluster. Otherwise, an empty string can be supplied. |
CollectionName |
The name of the collection in the database monitored by the trigger for changes. A valid value is required unless TriggerLevel is set to MonitorLevel.Database or MonitorLevel.Cluster. Otherwise, an empty string can be supplied. |
ConnectionStringSetting |
The name of an app setting or setting collection that specifies how to connect to the Azure DocumentDB cluster. |
TriggerLevel |
Indicates the level at which changes are monitored. Valid values of MonitorLevel are: Collection, Database, and Cluster. |
Usage
Use the TriggerLevel parameter to set the scope of changes being monitored.
[FunctionName(nameof(DocumentDBTrigger))]
public static void Run(
[CosmosDBMongoTrigger(databaseName: "",
collectionName: "",
ConnectionStringSetting = "<name-of-app-setting>",
TriggerLevel = MonitorLevel.Cluster)]
ChangeStreamDocument<BsonDocument> change,
ILogger logger)
{
logger.LogInformation("C# Azure DocumentDB trigger function processed a change.");
logger.LogInformation($"{change.FullDocument}");
}