【问题标题】:Retrieve document from collection in azure function cosmosdb trigger从 azure 函数 cosmosdb 触发器中的集合中检索文档
【发布时间】:2019-08-05 14:05:15
【问题描述】:

我创建了一个 azure 函数,当新文档添加到集合时会触发该函数。

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);
}}

是否可以从该集合中选择一个特定的文档,然后查询该所选文档中的数据?

例如。在名为clothingcollection 的集合中,我有一个id 为:12345Tops 的文档。我想查询文档中找到的id为:12345Tops的数据。

或者检索集合中的第一个文档,然后查询第一个选择的文档

我查看了带有 http 触发器的 azure 函数:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb#trigger---attributes

但我需要使用 cosmosdb 触发器,因为这需要在将文档添加到集合时触发。

【问题讨论】:

    标签: azure-functions azure-cosmosdb


    【解决方案1】:

    如果我理解正确,您想根据第一个集合上发生的更改查询第二个集合上的文档吗?

    这当然可行,您需要使用Cosmos DB Input Binding and pull the DocumentClient instance

    代码如下所示:

    [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");
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。不,我想从集合中选择特定文档并查询该特定文档中的数据。这将在 Azure Function cosmosdb 触发器中完成
    • 触发器发送一批文档,正如我在响应中显示的那样,您可以使用 DocumentClient 执行您想要的任何操作,它可以是查询,或者如果您想读取单个文档,您可以使用ReadDocumentAsync 而不是CreateDocumentQuery
    • 如果您不知道所需文档的 id 但您知道该 id 包含顶部,是否可以检索该文档?谢谢
    • 然后您可以像我最初的示例一样进行查询,在 WHERE 中使用 CONTAINS 过滤器。此时,您有 DocumentClient 根据来自触发器的信息发出您需要的所有查询/读取
    【解决方案2】:

    我认为您不必调用 CosmosDb 即可从 CosmosDb 触发器中检索完整文档。

    您应该能够获取 json 并反序列化为您想要的类型。 请看下面的示例函数。

    您可以执行 ToString() 从传递给函数的文档列表中提取文档内容。或者您可以直接将 json 反序列化为您拥有的对象。

    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();               
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 2020-07-07
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多