DocumentDB 中的数据跟踪

Data tracking in DocumentDB

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

例如,如果我在文档中有一个名为 Name 的 属性,其值为“Pieter”。现在我将其更改为“Sam”,我必须维护历史记录,之前是 "Pieter"。

到目前为止,我正在考虑预触发。还有其他解决方案吗?

如果您正在尝试创建审核日志,我建议您查看事件 Sourcing.Building 您的域来自事件以确保日志正确。参见 https://msdn.microsoft.com/en-us/library/dn589792.aspx and http://www.martinfowler.com/eaaDev/EventSourcing.html

Cosmos DB(以前称为 DocumentDB)现在通过更改源提供更改跟踪。使用 Change Feed,您可以侦听特定集合的更改,按分区内的修改排序。

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

  • Azure 函数
  • DocumentDB (SQL) SDK
  • 更改 Feed 处理器库

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

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