Skip to content

Commit f0203a6

Browse files
authored
Merge pull request #299655 from ggailey777/mongodb
[Functions][Clean PR] Add MongoDB binding extension docs (C# only)
2 parents 7b7d50d + e4f08a3 commit f0203a6

7 files changed

+307
-11
lines changed

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@
158158
"branch": "dev",
159159
"branch_mapping": {}
160160
},
161+
{
162+
"path_to_root": "azure-functions-mongodb-extension",
163+
"url": "https://github.com/Azure/Azure-functions-mongodb-extension",
164+
"branch": "main",
165+
"branch_mapping": {}
166+
},
161167
{
162168
"path_to_root": "azure-functions-nodejs-v4",
163169
"url": "https://github.com/Azure/azure-functions-nodejs-samples",

articles/azure-functions/TOC.yml

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -818,27 +818,40 @@
818818
displayName: Azure Cache for Redis
819819
- name: Azure Cosmos DB
820820
items:
821-
- name: Functions 1.x
822-
href: functions-bindings-cosmosdb.md
823-
displayName: Azure Cosmos DB
824-
- name: Functions 2.x and higher
825-
displayName: Azure Cosmos DB
821+
- name: API for NoSQL
826822
items:
827823
- name: Overview
828824
href: functions-bindings-cosmosdb-v2.md
829-
displayName: Azure Cosmos DB
825+
displayName: Azure Cosmos DB for NoSQL
830826
- name: Trigger
831827
href: functions-bindings-cosmosdb-v2-trigger.md
832-
displayName: Azure Cosmos DB
828+
displayName: Azure Cosmos DB for NoSQL
833829
- name: Input
834830
href: functions-bindings-cosmosdb-v2-input.md
835-
displayName: Azure Cosmos DB
831+
displayName: Azure Cosmos DB for NoSQL
836832
- name: Output
837833
href: functions-bindings-cosmosdb-v2-output.md
834+
displayName: Azure Cosmos DB for NoSQL
835+
- name: Functions 1.x (legacy)
836+
href: functions-bindings-cosmosdb.md
838837
displayName: Azure Cosmos DB
839-
- name: Migrate version 3.x to 4.x
840-
href: migrate-cosmos-db-version-3-version-4.md
841-
displayName: Azure Cosmos DB
838+
- name: Migrate version 3.x to 4.x
839+
href: migrate-cosmos-db-version-3-version-4.md
840+
displayName: Azure Cosmos DB for NoSQL
841+
- name: API for MongoDB (vCore)
842+
items:
843+
- name: Overview
844+
href: functions-bindings-mongodb-vcore.md
845+
displayName: Azure Cosmos DB for MongoDB (vCore)
846+
- name: Trigger
847+
href: functions-bindings-mongodb-vcore-trigger.md
848+
displayName: Azure Cosmos DB for MongoDB (vCore)
849+
- name: Input
850+
href: functions-bindings-mongodb-vcore-input.md
851+
displayName: Azure Cosmos DB for MongoDB (vCore)
852+
- name: Output
853+
href: functions-bindings-mongodb-vcore-output.md
854+
displayName: Azure Cosmos DB for MongoDB (vCore)
842855
- name: Azure Data Explorer
843856
items:
844857
- name: Overview
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: Azure Cosmos DB 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 retrieve 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)
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)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Azure Cosmos DB for MongoDB (vCore) Output Binding for Azure Functions
3+
description: Understand how to use Azure Cosmos DB for MongoDB (vCore) output to write new items to 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) output 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) output binding in Azure Functions.
15+
16+
The Azure Cosmos DB for MongoDB (vCore) output binding lets you write a new document to an Azure Cosmos DB for MongoDB(vCore) collection.
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 `CosmosDBMongoCollector` to add an item to the database:
23+
24+
```csharp
25+
[FunctionName("OutputBindingSample")]
26+
public static async Task OutputBindingRun(
27+
[TimerTrigger("*/5 * * * * *")] TimerInfo myTimer,
28+
[CosmosDBMongo("%vCoreDatabaseBinding%", "%vCoreCollectionBinding%", ConnectionStringSetting = "vCoreConnectionStringBinding")] IAsyncCollector<TestClass> CosmosDBMongoCollector,
29+
ILogger log)
30+
{
31+
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
32+
33+
TestClass item = new TestClass()
34+
{
35+
id = Guid.NewGuid().ToString(),
36+
SomeData = "some random data"
37+
};
38+
await CosmosDBMongoCollector.AddAsync(item);
39+
}
40+
```
41+
42+
The examples refer to a simple `TestClass` type:
43+
44+
```cs
45+
namespace Sample
46+
{
47+
public class TestClass
48+
{
49+
public string id { get; set; }
50+
public string SomeData { get; set; }
51+
}
52+
}
53+
```
54+
55+
## Attributes
56+
57+
This table describes the binding configuration properties of the `CosmosDBMongoTrigger` attribute.
58+
59+
|Parameter | Description|
60+
|---------|----------------------|
61+
|**FunctionId** | (Optional) The ID of the trigger function. |
62+
|**DatabaseName** | The name of the database being monitored by the trigger for changes. |
63+
|**CollectionName** | The name of the collection in the database being monitored by the trigger for changes.|
64+
|**ConnectionStringSetting** | The name of an app setting or setting collection that specifies how to connect to the Azure Cosmos DB account being monitored. |
65+
|**CreateIfNotExists** | (Optional) When set to true, creates the targeted database and collection when they don't already exist. |
66+
67+
## Usage
68+
69+
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:
70+
71+
:::code language="csharp" source="~/azure-functions-mongodb-extension/Sample/Sample.cs" range="17-29" :::
72+
73+
## Related articles
74+
75+
- [Azure Cosmos DB for MongoDB (vCore)](/azure/cosmos-db/mongodb/vcore/introduction)
76+
- [Azure Cosmos DB for MongoDB (vCore) bindings for Azure Functions](functions-bindings-mongodb-vcore.md)
77+
- [Azure Cosmos DB for MongoDB (vCore) trigger for Azure Functions](functions-bindings-mongodb-vcore-trigger.md)
78+
- [Azure Cosmos DB for MongoDB (vCore) input binding for Azure Functions](functions-bindings-mongodb-vcore-input.md)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Azure Cosmos DB for MongoDB (vCore) Trigger for Azure Functions
3+
description: Understand how to use Azure Cosmos DB for MongoDB (vCore) trigger to monitor change streams for inserts and updates in collections.
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) trigger 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) trigger in Azure Functions. The bindings use [change streams in Azure Cosmos DB’s API for MongoDB](/azure/cosmos-db/mongodb/change-streams) to listen for inserts and updates.
15+
16+
The change feed publishes only new and updated items. Watching for delete operations using change streams is currently not supported.
17+
18+
[!INCLUDE [functions-bindings-mongodb-vcore-preview](../../includes/functions-bindings-mongodb-vcore-preview.md)]
19+
20+
## Example
21+
22+
This example shows a function that returns a single document that is inserted or updated:
23+
24+
:::code language="csharp" source="~/azure-functions-mongodb-extension/Sample/Sample.cs" range="49-57" :::
25+
26+
For the complete example, see [Sample.cs](https://github.com/Azure/Azure-functions-mongodb-extension/blob/main/Sample/Sample.cs) in the extension repository.
27+
28+
## Attributes
29+
30+
This table describes the binding configuration properties of the `CosmosDBMongoTrigger` attribute.
31+
32+
|Parameter | Description|
33+
|---------|----------------------|
34+
|**FunctionId** | (Optional) The ID of the trigger function. |
35+
|**DatabaseName** | The name of the database being monitored by the trigger for changes. Required unless `TriggerLevel` is set to `MonitorLevel.Cluser`. |
36+
|**CollectionName** | The name of the collection in the database being monitored by the trigger for changes. Required when `TriggerLevel` is set to `MonitorLevel.Collection`.|
37+
|**ConnectionStringSetting** | The name of an app setting or setting collection that specifies how to connect to the Azure Cosmos DB account being monitored. |
38+
|**TriggerLevel** | Indicates the level at which changes are being monitored. Valid values of `MonitorLevel` are: `Collection`, `Database`, and `Cluster`. |
39+
40+
## Usage
41+
42+
Use the `TriggerLevel` parameter to set the scope of changes being monitored.
43+
44+
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:
45+
46+
:::code language="csharp" source="~/azure-functions-mongodb-extension/Sample/Sample.cs" range="17-29" :::
47+
48+
## Related articles
49+
50+
- [Azure Cosmos DB for MongoDB (vCore)](/azure/cosmos-db/mongodb/vcore/introduction)
51+
- [Azure Cosmos DB for MongoDB (vCore) bindings for Azure Functions](functions-bindings-mongodb-vcore.md)
52+
- [Azure Cosmos DB for MongoDB (vCore) input binding for Azure Functions](functions-bindings-mongodb-vcore-input.md)
53+
- [Azure Cosmos DB for MongoDB (vCore) output binding for Azure Functions](functions-bindings-mongodb-vcore-output.md)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Azure Cosmos DB for MongoDB (vCore) bindings for Azure Functions
3+
description: Learn to use the Azure Cosmos DB for MongoDB (vCore) trigger 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+
The Azure Cosmos DB for MongoDB (vCore) extension supports trigger, input, and output bindings for Azure Cosmos DB for MongoDB (vCore).
15+
16+
[!INCLUDE [functions-bindings-mongodb-vcore-preview](../../includes/functions-bindings-mongodb-vcore-preview.md)]
17+
18+
Using the Azure Cosmos DB for MongoDB (vCore) extension, you can build functions that can:
19+
20+
| Action | Trigger/binding type |
21+
|---------|-----------|
22+
| Execute on changes to a collection | [Azure Cosmos DB for MongoDB (vCore) trigger](functions-bindings-mongodb-vcore-trigger.md) |
23+
| Write documents to the database | [Azure Cosmos DB for MongoDB (vCore) output binding](functions-bindings-mongodb-vcore-output.md)|
24+
| Query the database | [Azure Cosmos DB for MongoDB (vCore) input binding](functions-bindings-mongodb-vcore-input.md) |
25+
26+
Considerations for the Azure Cosmos DB for MongoDB (vCore) extension:
27+
+ Only [C# apps that run in-proces with the host](./functions-dotnet-class-library.md) are currently supported in preview.
28+
+ The Azure Cosmos DB for MongoDB (vCore) binding extension doesn't currently support Microsoft Entra authentication and managed identities.
29+
+ Your app must be using version 4.x of the Azure Functions runtime.
30+
31+
## Install extension
32+
33+
Add the extension to your .NET project for an in-process app by installing [this preview NuGet package](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.AzureCosmosDb.Mongo/1.1.0-preview):
34+
35+
`Microsoft.Azure.WebJobs.Extensions.AzureCosmosDb.Mongo`
36+
37+
>[!NOTE]
38+
>Don't try to install this package in a .NET isolated worker process app. There will be errors and the app project won't build. To learn how to create a .NET app that uses the in-process model, see [Develop C# class library functions using Azure Functions](functions-dotnet-class-library.md#develop-c-class-library-functions-using-azure-functions).
39+
40+
## Related articles
41+
42+
- [What is Azure Cosmos DB for MongoDB (vCore architecture)?](/azure/cosmos-db/mongodb/vcore/introduction)
43+
- [Change streams in Azure Cosmos DB’s API for MongoDB](/azure/cosmos-db/mongodb/change-streams)
44+
- [Azure Cosmos DB for MongoDB (vCore) trigger for Azure Functions](functions-bindings-mongodb-vcore-trigger.md)
45+
- [Azure Cosmos DB for MongoDB (vCore) input binding for Azure Functions](functions-bindings-mongodb-vcore-input.md)
46+
- [Azure Cosmos DB for MongoDB (vCore) output binding for Azure Functions](functions-bindings-mongodb-vcore-output.md)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
author: ggailey777
3+
ms.service: azure-functions
4+
ms.topic: include
5+
ms.date: 05/12/2025
6+
ms.author: glenga
7+
---
8+
>[!IMPORTANT]
9+
>The Azure Cosmos DB for MongoDB (vCore) extension is currently in preview.
10+
>At this time, only .NET apps that use the [in-process model](../articles/azure-functions/functions-dotnet-class-library.md) are supported.

0 commit comments

Comments
 (0)