MongoDB/NoSQL:保存文档更改历史 [英] MongoDB/NoSQL: Keeping Document Change History

查看:20
本文介绍了MongoDB/NoSQL:保存文档更改历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库应用程序中一个相当普遍的要求是跟踪数据库中一个或多个特定实体的更改.我听说这称为行版本控制、日志表或历史表(我确定它还有其他名称).在 RDBMS 中有多种方法可以处理它——您可以将所有源表中的所有更改写入单个表(更多是日志),或者为每个源表创建一个单独的历史表.您还可以选择管理应用程序代码中的日志记录或通过数据库触发器.

A fairly common requirement in database applications is to track changes to one or more specific entities in a database. I've heard this called row versioning, a log table or a history table (I'm sure there are other names for it). There are a number of ways to approach it in an RDBMS--you can write all changes from all source tables to a single table (more of a log) or have a separate history table for each source table. You also have the option to either manage the logging in application code or via database triggers.

我正在尝试思考在 NoSQL/文档数据库(特别是 MongoDB)中解决相同问题的方法,以及如何以统一的方式解决它.它会像为文档创建版本号一样简单,并且从不覆盖它们吗?为真实"和记录"文档创建单独的集合?这将如何影响查询和性能?

I'm trying to think through what a solution to the same problem would look like in a NoSQL/document database (specifically MongoDB), and how it would be solved in a uniform way. Would it be as simple as creating version numbers for documents, and never overwriting them? Creating separate collections for "real" vs. "logged" documents? How would this affect querying and performance?

无论如何,这是否是 NoSQL 数据库的常见场景,如果是,是否有通用解决方案?

Anyway, is this a common scenario with NoSQL databases, and if so, is there a common solution?

推荐答案

好问题,我自己也在研究这个问题.

Good question, I was looking into this myself as well.

我遇到了用于 Ruby 的 Mongoid 驱动程序的版本控制模块.我自己没有使用过,但是从 我能找到的,它为每个文档添加一个版本号.旧版本嵌入在文档本身中.主要缺点是每次更改都会复制整个文档,这将导致在处理大型文档时存储大量重复的内容.虽然当您处理小型文档和/或不经常更新文档时,这种方法很好.

I came across the Versioning module of the Mongoid driver for Ruby. I haven't used it myself, but from what I could find, it adds a version number to each document. Older versions are embedded in the document itself. The major drawback is that the entire document is duplicated on each change, which will result in a lot of duplicate content being stored when you're dealing with large documents. This approach is fine though when you're dealing with small-sized documents and/or don't update documents very often.

另一种方法是仅将更改的字段存储在新版本中.然后,您可以展平"您的历史记录以重建文档的任何版本.不过,这相当复杂,因为您需要跟踪模型中的更改并以应用程序可以重建最新文档的方式存储更新和删除.这可能很棘手,因为您处理的是结构化文档而不是平面 SQL 表.

Another approach would be to store only the changed fields in a new version. Then you can 'flatten' your history to reconstruct any version of the document. This is rather complex though, as you need to track changes in your model and store updates and deletes in a way that your application can reconstruct the up-to-date document. This might be tricky, as you're dealing with structured documents rather than flat SQL tables.

每个字段也可以有单独的历史记录.通过这种方式,将文档重建为给定版本要容易得多.在您的应用程序中,您不必显式跟踪更改,而只需在更改其值时创建属性的新版本.文档可能如下所示:

Each field can also have an individual history. Reconstructing documents to a given version is much easier this way. In your application you don't have to explicitly track changes, but just create a new version of the property when you change its value. A document could look something like this:

{
  _id: "4c6b9456f61f000000007ba6"
  title: [
    { version: 1, value: "Hello world" },
    { version: 6, value: "Foo" }
  ],
  body: [
    { version: 1, value: "Is this thing on?" },
    { version: 2, value: "What should I write?" },
    { version: 6, value: "This is the new body" }
  ],
  tags: [
    { version: 1, value: [ "test", "trivial" ] },
    { version: 6, value: [ "foo", "test" ] }
  ],
  comments: [
    {
      author: "joe", // Unversioned field
      body: [
        { version: 3, value: "Something cool" }
      ]
    },
    {
      author: "xxx",
      body: [
        { version: 4, value: "Spam" },
        { version: 5, deleted: true }
      ]
    },
    {
      author: "jim",
      body: [
        { version: 7, value: "Not bad" },
        { version: 8, value: "Not bad at all" }
      ]
    }
  ]
}

在版本中将文档的一部分标记为已删除仍然有些尴尬.您可以为可以从应用程序中删除/恢复的部分引入 state 字段:

Marking part of the document as deleted in a version is still somewhat awkward though. You could introduce a state field for parts that can be deleted/restored from your application:

{
  author: "xxx",
  body: [
    { version: 4, value: "Spam" }
  ],
  state: [
    { version: 4, deleted: false },
    { version: 5, deleted: true }
  ]
}

使用这些方法中的每一种,您都可以将最新的扁平化版本存储在一个集合中,并将历史数据存储在一个单独的集合中.如果您只对文档的最新版本感兴趣,这应该会缩短查询时间.但是当您需要最新版本和历史数据时,您需要执行两个查询,而不是一个.因此,选择使用单个集合还是使用两个单独的集合应该取决于您的应用程序需要历史版本的频率.

With each of these approaches you can store an up-to-date and flattened version in one collection and the history data in a separate collection. This should improve query times if you're only interested in the latest version of a document. But when you need both the latest version and historical data, you'll need to perform two queries, rather than one. So the choice of using a single collection vs. two separate collections should depend on how often your application needs the historical versions.

这个答案的大部分只是我的想法,我还没有真正尝试过.回想起来,第一个选项可能是最简单和最好的解决方案,除非重复数据的开销对您的应用程序非常重要.第二种选择非常复杂,可能不值得付出努力.第三个选项基本上是对选项二的优化,应该更容易实现,但可能不值得付出努力,除非你真的不能选择选项一.

Most of this answer is just a brain dump of my thoughts, I haven't actually tried any of this yet. Looking back on it, the first option is probably the easiest and best solution, unless the overhead of duplicate data is very significant for your application. The second option is quite complex and probably isn't worth the effort. The third option is basically an optimization of option two and should be easier to implement, but probably isn't worth the implementation effort unless you really can't go with option one.

期待对此问题的反馈,以及其他人对该问题的解决方案:)

Looking forward to feedback on this, and other people's solutions to the problem :)

这篇关于MongoDB/NoSQL:保存文档更改历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆