【问题标题】:Data tracking in DocumentDBDocumentDB 中的数据跟踪
【发布时间】:2016-08-30 21:00:58
【问题描述】:

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

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

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

【问题讨论】:

    标签: azure azure-storage azure-cosmosdb


    【解决方案1】:

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

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

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

    例如,这里是来自 Change Feed 文档的 sn-p,用于从 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;
            }
    

    【讨论】:

      【解决方案2】:

      如果您尝试制作审核日志,我建议您查看事件溯源。根据事件构建您的域可确保正确的日志。见https://msdn.microsoft.com/en-us/library/dn589792.aspx和http://www.martinfowler.com/eaaDev/EventSourcing.html

      【讨论】:

      • 解决方案非常好,但我认为当涉及到我需要的要求时它更复杂。我只想跟踪另一个集合中的详细信息。我有什么办法与属性有关吗?就像,如果我们将 [Track] 属性添加到 Name 属性,将记录更改。还是这样?
      猜你喜欢
      • 2017-09-24
      • 1970-01-01
      • 2013-02-12
      • 2018-07-07
      • 2016-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多