在MongoDB中实现数据版本控制的方法 [英] Ways to implement data versioning in MongoDB

查看:409
本文介绍了在MongoDB中实现数据版本控制的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否分享您的想法,如何在MongoDB中实现数据版本控制. (我问过关于Cassandra的类似问题.如果您有任何疑问,请考虑分享哪个数据库更合适)

Can you share your thoughts how would you implement data versioning in MongoDB. (I've asked similar question regarding Cassandra. If you have any thoughts which db is better for that please share)

假设我需要在一个简单的通讯录中对记录进行版本控制. (地址簿记录存储为平面json对象).我希望有这样的历史:

Suppose that I need to version records in an simple address book. (Address book records are stored as flat json objects). I expect that the history:

  • 将很少使用
  • 将立即全部使用,以时间机器"的方式呈现
  • 单个记录的版本不会超过几百个. 历史不会过期.
  • will be used infrequently
  • will be used all at once to present it in a "time machine" fashion
  • there won't be more versions than few hundred to a single record. history won't expire.

我正在考虑以下方法:

  • 创建一个新的对象集合以存储记录的历史记录或对记录的更改.它将在每个版本中存储一个对象,并引用地址簿条目.这样的记录如下:

  • Create a new object collection to store history of records or changes to the records. It would store one object per version with a reference to the address book entry. Such records would looks as follows:


{
 '_id': 'new id',
 'user': user_id,
 'timestamp': timestamp,
 'address_book_id': 'id of the address book record' 
 'old_record': {'first_name': 'Jon', 'last_name':'Doe' ...}
}

可以修改此方法,以便为每个文档存储一组版本.但这似乎是一种较慢的方法,没有任何优势.

This approach can be modified to store an array of versions per document. But this seems to be slower approach without any advantages.

将版本存储为附加到地址簿条目的序列化(JSON)对象.我不确定如何将此类对象附加到MongoDB文档.也许作为字符串数组. (在使用CouchDB进行简单文档版本控制后进行了建模)

Store versions as serialized (JSON) object attached to address book entries. I'm not sure how to attach such objects to MongoDB documents. Perhaps as an array of strings. (Modelled after Simple Document Versioning with CouchDB)

推荐答案

深入探讨此问题时的第一个大问题是您如何存储变更集" ?

The first big question when diving in to this is "how do you want to store changesets"?

  1. 有什么不同吗?
  2. 完整记录副本?

我个人的做法是存储差异.因为显示这些差异确实是一个特殊的动作,所以我将这些差异放入另一个历史"集合中.

My personal approach would be to store diffs. Because the display of these diffs is really a special action, I would put the diffs in a different "history" collection.

我将使用其他集合来节省内存空间.通常,您不希望为简单查询提供完整的历史记录.因此,通过将历史记录保留在对象之外,您还可以在查询数据时将其保留在通常访问的内存之外.

I would use the different collection to save memory space. You generally don't want a full history for a simple query. So by keeping the history out of the object you can also keep it out of the commonly accessed memory when that data is queried.

为了使我的生活更轻松,我将使历史文档包含带有时间戳记的差异字典.像这样:

To make my life easy, I would make a history document contain a dictionary of time-stamped diffs. Something like this:

{
    _id : "id of address book record",
    changes : { 
                1234567 : { "city" : "Omaha", "state" : "Nebraska" },
                1234568 : { "city" : "Kansas City", "state" : "Missouri" }
               }
}

为了使我的生活变得真正轻松,我将把这部分数据对象(用于访问数据)用作DataObjects(EntityWrapper,无论如何).通常,这些对象具有某种形式的历史记录,因此您可以轻松地覆盖save()方法来同时进行此更改.

To make my life really easy, I would make this part of my DataObjects (EntityWrapper, whatever) that I use to access my data. Generally these objects have some form of history, so that you can easily override the save() method to make this change at the same time.

更新日期:2015-10

看起来现在有处理JSON差异的规范.这似乎是存储差异/更改的更可靠的方法.

It looks like there is now a spec for handling JSON diffs. This seems like a more robust way to store the diffs / changes.

这篇关于在MongoDB中实现数据版本控制的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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