Explain how would you keep document change history in NoSQL DB?

Technology CommunityCategory: NoSQLExplain how would you keep document change history in NoSQL DB?
VietMX Staff asked 3 years ago

There are some solution for that: 1. Create a new version of the document on each change – Add a version number to each document on change. The major drawback is that the entire document is duplicated on each change, which will result in a lot of duplicate content being stored when you’re dealing with large documents. This approach is fine though when you’re dealing with small-sized documents and/or don’t update documents very often. 2. Only store changes in a new version – For that store only the changed fields in a new version. Then you can ‘flatten’ your history to reconstruct any version of the document. This is rather complex though, as you need to track changes in your model and store updates and deletes in a way that your application can reconstruct the up-to-date document. This might be tricky, as you’re dealing with structured documents rather than flat SQL tables. 3. Store changes within the document – Each field can also have an individual history. Reconstructing documents to a given version is much easier this way. In your application you don’t have to explicitly track changes, but just create a new version of the property when you change its value.

{
  _id: "4c6b9456f61f000000007ba6"
  title: [
    { version: 1, value: "Hello world" },
    { version: 6, value: "Foo" }
  ],
  body: [
    { version: 1, value: "Is this thing on?" },
    { version: 2, value: "What should I write?" },
    { version: 6, value: "This is the new body" }
  ],
  tags: [
    { version: 1, value: [ "test", "trivial" ] },
    { version: 6, value: [ "foo", "test" ] }
  ],
  comments: [
    {
      author: "joe", // Unversioned field
      body: [
        { version: 3, value: "Something cool" }
      ]
    },
    {
      author: "xxx",
      body: [
        { version: 4, value: "Spam" },
        { version: 5, deleted: true }
      ]
    },
    {
      author: "jim",
      body: [
        { version: 7, value: "Not bad" },
        { version: 8, value: "Not bad at all" }
      ]
    }
  ]
}
  1. Variation on Store changes within the document – Instead of storing versions against each key pair, the current key pairs in the document always represents the most recent state and a ‘log’ of changes is stored within a history array. Only those keys which have changed since creation will have an entry in the log.
{
  _id: "4c6b9456f61f000000007ba6"
  title: "Bar",
  body: "Is this thing on?",
  tags: [ "test", "trivial" ],
  comments: [
    { key: 1, author: "joe", body: "Something cool" },
    { key: 2, author: "xxx", body: "Spam", deleted: true },
    { key: 3, author: "jim", body: "Not bad at all" }
  ],
  history: [
    { 
      who: "joe",
      when: 20160101,
      what: { title: "Foo", body: "What should I write?" }
    },
    { 
      who: "jim",
      when: 20160105,
      what: { tags: ["test", "test2"], comments: { key: 3, body: "Not baaad at all" }
    }
  ]
}