DocumentDB中的数据跟踪

时间:2016-05-05 06:59:22

标签: azure azure-storage azure-cosmosdb

我试图保留DocumentDB的数据历史(至少退一步)。

例如,如果我在文档中有一个名为Name的属性,其值为" Pieter"。现在我正在将其改为" Sam",我必须保持历史,它是"彼得"先前。

截至目前,我正在考虑预先触发。还有其他解决方案吗?

2 个答案:

答案 0 :(得分:0)

如果您正在尝试制作审核日志,我建议您查看事件采购。从事件中构建您的域可确保正确记录日志。请参阅https://msdn.microsoft.com/en-us/library/dn589792.aspxhttp://www.martinfowler.com/eaaDev/EventSourcing.html

答案 1 :(得分:0)

Cosmos DB(以前的DocumentDB)现在通过Change Feed提供更改跟踪。使用Change Feed,您可以侦听特定集合的更改,通过在分区内进行修改来订购。

可通过以下方式访问更改Feed:

  • Azure功能
  • DocumentDB(SQL)SDK
  • 更改Feed处理器库

例如,对于给定的分区(文档here中的完整代码示例),这里是来自Change Feed文档的片段(从更改Feed中读取):

IDocumentQuery<Document> query = client.CreateDocumentChangeFeedQuery(
        collectionUri,
        new ChangeFeedOptions
        {
            PartitionKeyRangeId = pkRange.Id,
            StartFromBeginning = true,
            RequestContinuation = continuation,
            MaxItemCount = -1,
            // Set reading time: only show change feed results modified since StartTime
            StartTime = DateTime.Now - TimeSpan.FromSeconds(30)
        });
    while (query.HasMoreResults)
        {
            FeedResponse<dynamic> readChangesResponse = query.ExecuteNextAsync<dynamic>().Result;

            foreach (dynamic changedDocument in readChangesResponse)
                {
                     Console.WriteLine("document: {0}", changedDocument);
                }
            checkpoints[pkRange.Id] = readChangesResponse.ResponseContinuation;
        }