Azure CosmosDB 触发器,用于基于另一个集合中的值更新集合



我必须根据另一个 cosmosDB 集合(let be collection-2)中的列值更新 cosmosDB 集合(let be collection-1)。集合 1 应使用其他集合(如集合 3 和集合 4)中的值进行更新。我尝试在 Collection-2 中编写 post 触发器,但在触发器内编写函数时卡住了。

请建议是否可以使用 CosmosDB 触发器或建议是否有任何其他方法来实现此目的。

我为 cosmos DB 创建了一个新的触发函数。

据我所知,只能在每个 Azure 函数 CosmosDB 触发器中监视一个 Cosmos DB 集合的更新。

换句话说,如果 Collection-1 中的列更新是由集合 2、3、4 的更新决定的,则需要为此创建 3 个触发器。

在每个触发器中,请按照此文档配置集合信息,并使用 Cosmos DB SDK 替换特定文档。


更新答案:

完全同意@Matias Quaranta评论中的解释,您在这里混淆了两种触发器。正如你提到的,当然需要采用 Azure 函数触发器。Cosmos DB 触发器无法监视集合的任何更新,它是被动触发的。

例如:

如果要在将文档插入 cosmos db之前添加一列,可以设置触发器名称以在使用插入文档 cosmos db sdk 时激活它。这是在宇宙数据库下触发的。

如果要监视 cosmos db 集合的更新,请执行一些业务,需要采用 Azure 函数触发器。

我对集合>>>触发器和Azure函数Cosmos DB Trigger感到困惑。 我上述问题的解决方案是创建一个 Azure 函数来触发 cosmos DB。

创建资源>>>计算>>>函数应用。

要添加输入和输出,请转到已创建的函数应用>>>触发器中的"集成"选项。

有关函数绑定,请参阅链接:

此函数将在单个 cosmosDB 集合中的数据更新时触发。 我们可以添加 n 个集合作为输入和一个集合作为输出。

下面是在定义绑定时自动创建的 function.json 文件:

{
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases",
"connectionStringSetting": "cosmosdb_DOCUMENTDB",
"databaseName": "connectivityDB",
"collectionName": "tblEvent",
"createLeaseCollectionIfNotExists": true
},
{
"type": "cosmosDB",
"name": "outputDocument",
"databaseName": "connectivityDB",
"collectionName": "tblNewEvent",
"createIfNotExists": true,
"connectionStringSetting": "cosmosdb_DOCUMENTDB",
"partitionKey": "/id",
"direction": "out"
}
]
}

以下是创建的索引.js文件:

module.exports = function(context, input) {
var documents = context.bindings.documents;
var output = [];
if(!!input && input.length > 0){
//write your code here
}
context.bindings.outputDocument = output;
context.done();
}

最新更新