本文翻译自:Ways to implement data versioning in MongoDB
Can you share your thoughts how would you implement data versioning in MongoDB. 您能否分享您的想法,如何在MongoDB中实现数据版本控制。 (I've asked similar question regarding Cassandra . If you have any thoughts which db is better for that please share) (我也曾问过有关Cassandra的类似问题 。如果您有任何想法,哪个数据库更好,请分享)
Suppose that I need to version records in an simple address book. 假设我需要在一个简单的通讯簿中对记录进行版本控制。 (Address book records are stored as flat json objects). (地址簿记录存储为平面json对象)。 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. 历史不会过期。
I'm considering the following approaches: 我正在考虑以下方法:
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: 这样的记录如下:
\n{ { \n '_id': 'new id', '_id':'新ID',\n 'user': user_id, '用户':user_id,\n 'timestamp': timestamp, '时间戳':时间戳,\n 'address_book_id': 'id of the address book record' 'address_book_id':'通讯录记录的ID' \n 'old_record': {'first_name': 'Jon', 'last_name':'Doe' ...} 'old_record':{'first_name':'Jon','last_name':'Doe'...}\n} }\n
This approach can be modified to store an array of versions per document. 可以修改此方法以存储每个文档的版本数组。 But this seems