This repo contains binding extensions for the Azure WebJobs SDK intended for working with Azure CosmosDB's various APIs. See the Azure WebJobs SDK repo for more information on WebJobs.
The extension for working with the NoSQL API is located in the general WebJobs Extension repo
To configure the binding, add the Mongo connection string as an app setting or environment variable using the setting name CosmosDB. The name of the setting can be changed with the ConnectionStringKey property of the binding attribute.
The extension currently supports getting either a database or a collection.
public void Inputs(
[CosmosDBMongo(DatabaseName)] IMongoDatabase db,
[CosmosDBMongo(DatabaseName, CollectionName)] IMongoCollection<BsonDocument> coll)
{
Assert.IsNotNull(db);
Assert.IsNotNull(coll);
}In this example, the newItem object is upserted into the ItemCollection collection of the ItemDb database.
public static void InsertDocument(
[QueueTrigger("sample")] QueueData trigger,
[CosmosDBMongo("DatabaseName", "ItemCollection")] out BsonDocument newItem)
{
newItem = new BsonDocument();
}Simple C# objects can also be automatically converted into BSON. The following will write a simple document to the service.
public static void InsertDocument(
[QueueTrigger("sample")] QueueData trigger,
[CosmosDBMongo("DatabaseName", "ItemCollection")] out ItemDoc newItem)
{
newItem = new ItemDoc()
{
Text = "sample text"
};
}If you need more control, you can also specify a parameter of type IMongoClient. The following example uses MongoClient to query for all documents.
public static void DocumentClient(
[QueueTrigger("sample")] QueueData trigger,
[CosmosDBMongo] IMongoClient client,
TraceWriter log)
{
var documents = client.getDatabase("Database").getCollection<BsonDocument>("Collection").find();
foreach (BsonDocument d in documents)
{
log.Info(d);
}
}There is a separate attribute for writing functions which trigger on changes to a Cosmos Mongo collection. CosmosDBMongoTrigger has a few additional configurations available compared to the basic attribute. Because the trigger needs to keep track of which data has already been seen, it uses an additional collection. By default, it uses a collection named leases in the same database as the source collection. However LeaseConnectionStringKey, LeaseDatabaseName and LeaseCollectionName can be used if desired to place it elsewhere.
public static void Trigger(
[CosmosDBMongoTrigger(DatabaseName, MonitoredCollectionName)] IEnumerable<BsonDocument> docs,
ILogger logger)
{
foreach (BsonDocument doc in docs)
{
logger.LogInformation("Doc triggered");
}
}Until the extension becomes available in the portal and extension bundles, it can be used by directly installing the extension. Either use whatever tool/template to create a CosmosDB Trigger and modify it as follows, or use the example projects for C# and Typescript.
- Remove the extension bundle from
host.jsonif it exists. - Run
func extensions install --package Microsoft.Azure.WebJobs.Extensions.CosmosDb.Mongo --version 1.0.2 - Modify the
function.jsonand/or annotations to"type":"cosmosDBMongoTrigger""datatype":"binary"createLeaseCollectionIfNotExistscan be removed- Rename
connectionStringSettingtoconnectionStringKey
- Add the Mongo connection string as an app setting or environment variable using the setting name
CosmosDB - Change the function file itself to take a language specific binary type and deserialize it with the language specific BSON driver.