Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

Can you share your thoughts how would you implement data versioning in MongoDB? Suppose that I need to version records in a simple address book. (Address book records are stored as flat json objects). I expect that 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 a few hundred to a single record. history won't expire.

I'm considering the following approaches:

Create a new object collection to store a history of records or changes to the records. It would store one object perversion with a reference to the address book entry. Such records would look 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 a 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)

1 Answer

0 votes
by (106k points)

For implementing data versioning in MongoDB you can make a history document contains 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" } 

}

Browse Categories

...