Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to implement data versioning in MongoDB

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)

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.

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:

     {  '_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.

  • 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)

like image 972
Piotr Czapla Avatar asked Nov 15 '10 14:11

Piotr Czapla


People also ask

Does MongoDB have versioning?

This pattern addresses the problem of wanting to keep around older revisions of some documents in MongoDB instead of bringing in a second management system. To accomplish this, we add a field to each document allowing us to keep track of the document version.

How do I make a versioning database?

To effectively version a database, you must monitor and understand the changes that are occurring. Versioning a database means sharing all changes in a database that is necessary for other team members in order to get the project running properly.

Where do I put version control in a document?

A Version Control Table should be inserted on the front page of the document.


1 Answers

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

  1. Diffs?
  2. Whole record copies?

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

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.

UPDATE: 2015-10

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

like image 149
Gates VP Avatar answered Oct 04 '22 00:10

Gates VP