MongoDB/NoSQL: Keeping Document Change History

后端 未结 5 1820
余生分开走
余生分开走 2020-12-02 03:31

A fairly common requirement in database applications is to track changes to one or more specific entities in a database. I\'ve heard this called row versioning, a log table

5条回答
  •  醉话见心
    2020-12-02 04:32

    Why not a 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" }
        }
      ]
    }
    

提交回复
热议问题