Maintaining version history in MongoDB

I want to maintain version history of every field of every Document of every collection in MongoDB.

This is what I have so far - For a collection A, I want to maintain a collection A_revision.

 Collection A - 
  {
    _id: "abcd" : {
      key1: "value4",
      key2: "value2"
    }
  }

Collection A_revision -
{
   _id: "someid": {
       timestamp: 847393,
       userId: "someuserId",
       key1: "value3",
       key1_old: "value1",
       parentId: "abcd",
    }
   _id: "someid2": {
       timestamp: 948392,
       userId: "someuserId",
       key1: "value4",
       key1_old: "value3",
       parentId: "abcd",
    }
}

In the above example, user with userId "someuserId", changed the value of key1 from "value1" to "value3" to "value4".

Is this the best design of collection A_revision to go for it?

PS - We will be fetching revisions on demand (lazy fetch).

728x90

0 Answers Maintaining version history in MongoDB