我已经创建了一个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);
}}是否可以从该集合中选择特定文档,然后查询所选文档中的数据?
例如:在名为clothescollection的集合中,我有一个id为12345Tops的文档。我想查询在id为12345Tops的文档中找到的数据。
或者取回集合中的第一个文档,然后查询第一个选择的文档
我已经查看过带有http触发器的azure函数:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb#trigger---attributes
但我需要使用cosmosdb触发器,因为这需要在将文档添加到集合时触发。
发布于 2019-03-15 00:01:30
如果我理解正确的话,您想要根据第一个集合上发生的更改来查询第二个集合上的文档?
这当然是可行的,您需要使用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");
}
}
}发布于 2020-05-13 00:51:22
我不认为必须调用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();
}
}
}https://stackoverflow.com/questions/55166570
复制相似问题