首页 > 解决方案 > 检测更改提要中的单个文档属性更改

问题描述

我有一个文档类型 State 用于保存当前和历史“状态”数据。每次当前状态中的属性发生更改时,我都会向 States 集合添加一个新文档,因此会随着时间的推移创建状态的“快照”。

我想知道是否可以使用 Change Feed 和 CosmosDBTrigger 来检测 State 的各个属性的更改,以便我可以根据更改触发代码。

像这样的东西:

[FunctionName("StateChangeTrigger")]
public static void Run([CosmosDBTrigger(
    databaseName: "mydb",
    collectionName: "States",
    ConnectionStringSetting = "CosmosDB",
    LeaseCollectionName = "leases",
    CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> documents, ILogger log)
{
    if (documents != null)
    {
        Document currentDoc = documents[documents.Count-1];
        Document previousDoc;
        if (documents.Count == 1)
        {
            previousDoc = // Fetch previous document from database, based on timestamp
        }
        else
        {
            previousDoc = documents[documents.Count-2];
        }
        if (currentDoc.MyProperty != previousDoc.MyProperty) // Doesn't work but you get my drift
        {
            log.LogInformation("Property 'MyProperty' has been modified");
            // Do something
        }
    }
}

触发 CosmosDBTrigger 后,显然我们知道集合中有一个附加项,但我们并不确切知道哪些属性发生了变化。示例代码是处理此问题的有效方法吗?或者,还有更好的方法?

提前致谢!

标签: c#.netazure-cosmosdb

解决方案


推荐阅读