1
votes

I have created an azure function that gets triggered when a new document is added to a collection.

public static void Run(IReadOnlyList<Document> input, ILogger log)
{
if (input != null && input.Count > 0)
{
    log.LogInformation("Documents modified " + input.Count);
    log.LogInformation("First document Id " + input[0].Id);
}}

Is it possible to select a particular document from this collection and then query the data in that selected document?

Eg. in the collection called clothescollection, i have a document that has an id:12345Tops. I want to query the data found in the document with the id:12345Tops.

Or alternatively retrieve the 1st document in the collection, and then query that 1st selected document

i have viewed azure functions with http triggers: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb#trigger---attributes

but i need to use cosmosdb trigger as this needs to be triggered when a document is added to the collection.

2

2 Answers

1
votes

If I understand correctly, you want to query documents on a second collection based on the changes that happen on a first collection?

That is certainly doable, you need to use a Cosmos DB Input Binding and pull the DocumentClient instance.

The code would look something like:

[FunctionName("CosmosTrigger")]
public static void Run([CosmosDBTrigger(
        databaseName: "ToDoItems",
        collectionName: "Items",
        ConnectionStringSetting = "CosmosDBConnection",
        LeaseCollectionName = "leases",
        CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> documents, 
    [CosmosDB(
        databaseName: "ToDoItems",
        collectionName: "CollectionToQuery",
        ConnectionStringSetting = "CosmosDBConnection")] DocumentClient client,
    ILogger log)
{
    foreach (var documentInsertedOrUpdated in documents)
    {
        try
        {
            // Do a read document on another collection
            var otherDocument = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri("ToDoItems", "CollectionToQuery", "some-id-maybe-taking-it-from-the-documentInsertedOrUpdated"));
        }
        catch(Exception ex)
        {
            log.LogError(ex, "Failed to process document");
        }
    }
}
0
votes

I don't think you have to call CosmosDb to retrieve the full document from a CosmosDb trigger.

You should be able to get the json and deserialize to the type that you want. Please see the below sample function.

You could just do ToString() to extract the document content from the list of documents that's being passed to the function. Or you could directly deseralize the json into an object that you have.

public static class CosmosDbAuditFunction
{
    [FunctionName("CosmosDbAuditFunction")]
    public static void Run([CosmosDBTrigger(
        databaseName: "DBNAME",
        collectionName: "COLLECTIONNAME",
        ConnectionStringSetting = "CosmosDbConnectionString",
        LeaseCollectionName = "LeaseCollection",
        CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> input, ILogger log)
    {
        if (input != null && input.Count > 0)
        {
            var changedDocumentjson = input[0].ToString();               
        }
    }
}