title: Ways to implement data versioning in MongoDB
body: <p>Can you share your thoughts how would you implement data versioning in MongoDB. (I've asked <a href="https://stackoverflow.com/questions/4183945/ways-to-implement-data-versioning-in-cassandra">similar question regarding Cassandra</a>. If you have any thoughts which db is better for that please share)</p>
  
  <p>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:</p>
  
  <ul>
  <li>will be used infrequently</li>
  <li>will be used all at once to present it in a "time machine" fashion</li>
  <li>there won't be more versions than few hundred to a single record.
  history won't expire.</li>
  </ul>
  
  <p>I'm considering the following approaches:</p>
  
  <ul>
  <li><p>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:</p>
  
  <pre>
  {
   '_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' ...}
  }
  </pre>
  
  <p>This approach can be modified to store an array of versions per document. But this seems to be slower approach without any advantages.</p></li>
  <li><p>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.
  (<a href="http://blog.couchone.com/post/632718824/simple-document-versioning-with-couchdb" rel="noreferrer">Modelled after Simple Document Versioning with CouchDB</a>)</p></li>
  </ul>