0
votes

I've written an Azure function which halfway through the function, builds up a CosmosDb document query and excecutes it to find a document in the collection.

As I have the binding for the CosmosDB specified with no Id or Sql query supplied, does this retrieve the entire collection by default? If so is there a way to prevent this,as I only want to query the collection once I have built the document query and return a result.

Do I need to specify a direction of inout to achieve this?

   public static class SynchroniseMemberData
{

    [FunctionName("SynchroniseMemberData")]
    public static void Run([EventGridTrigger] EventGridEvent eventGridEvent, ILogger log,
        [CosmosDB(
                databaseName: "MembersData",
                collectionName: "Members",
                ConnectionStringSetting = "offers_DOCUMENTDB"),
        ]
        DocumentClient client,
        [CosmosDB(
            databaseName: "MembersData",
            collectionName: "Members",
            ConnectionStringSetting = "offers_DOCUMENTDB")
        ]
        out object document
    )
    {

        log.LogInformation(eventGridEvent.Data.ToString());

        MemberDetails memberDetails = JsonConvert.DeserializeObject<MemberDetails>(eventGridEvent.Data.ToString());
        Uri collectionUri = UriFactory.CreateDocumentCollectionUri("MembersData", "Members");
        var query = client.CreateDocumentQuery<Document>(collectionUri).Where(p => p.Id == memberDetails.id);
        var member = query.AsEnumerable().FirstOrDefault();

        if (member == null)
        {
            document = new
            {
               //Create New Member
            };
        }
        else
        {
           //update member details and save back
            document = member;
        }

    }
1
Do you have a CosmosDBTrigger function? Post your function code if possible.Chris B
added in the code, the actual trigger is from EventGridTriggergrunt121
What behaviour are you seeing to make you think it's getting the entire collection by default?Chris B
The documentation by Microsoftgrunt121
Sorry I meant - what part of the code is returning the entire collection? You are using the DocumentClient binding for the input so not actually getting any results for anything until you do your query?Chris B

1 Answers

1
votes

No, pulling the DocumentClient does not query the collection.

In the Bindings code, when you request the DocumentClient, it uses the CosmosDBClientBuilder to obtain the DocumentClient instance for the attribute values:

rule.BindToInput<DocumentClient>(new CosmosDBClientBuilder(this));

The CosmosDBClientBuilder will obtain the DocumentClient instance from the internal cache.

ICosmosDBService service = _configProvider.GetService(resolvedConnectionString, attribute.PreferredLocations, attribute.UseMultipleWriteLocations, attribute.UseDefaultJsonSerialization);
return service.GetClient();