Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1010263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:03:20+00:00 2026-05-16T09:03:20+00:00

A fairly common requirement in database applications is to track changes to one or

  • 0

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 or a history table (I’m sure there are other names for it). There are a number of ways to approach it in an RDBMS–you can write all changes from all source tables to a single table (more of a log) or have a separate history table for each source table. You also have the option to either manage the logging in application code or via database triggers.

I’m trying to think through what a solution to the same problem would look like in a NoSQL/document database (specifically MongoDB), and how it would be solved in a uniform way. Would it be as simple as creating version numbers for documents, and never overwriting them? Creating separate collections for “real” vs. “logged” documents? How would this affect querying and performance?

Anyway, is this a common scenario with NoSQL databases, and if so, is there a common solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:03:21+00:00Added an answer on May 16, 2026 at 9:03 am

    Good question, I was looking into this myself as well.

    Create a new version on each change

    I came across the Versioning module of the Mongoid driver for Ruby. I haven’t used it myself, but from what I could find, it adds a version number to each document. Older versions are embedded in the document itself. 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.

    Only store changes in a new version

    Another approach would be to 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.

    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. A document could look something like this:

    {
      _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" }
          ]
        }
      ]
    }
    

    Marking part of the document as deleted in a version is still somewhat awkward though. You could introduce a state field for parts that can be deleted/restored from your application:

    {
      author: "xxx",
      body: [
        { version: 4, value: "Spam" }
      ],
      state: [
        { version: 4, deleted: false },
        { version: 5, deleted: true }
      ]
    }
    

    With each of these approaches you can store an up-to-date and flattened version in one collection and the history data in a separate collection. This should improve query times if you’re only interested in the latest version of a document. But when you need both the latest version and historical data, you’ll need to perform two queries, rather than one. So the choice of using a single collection vs. two separate collections should depend on how often your application needs the historical versions.

    Most of this answer is just a brain dump of my thoughts, I haven’t actually tried any of this yet. Looking back on it, the first option is probably the easiest and best solution, unless the overhead of duplicate data is very significant for your application. The second option is quite complex and probably isn’t worth the effort. The third option is basically an optimization of option two and should be easier to implement, but probably isn’t worth the implementation effort unless you really can’t go with option one.

    Looking forward to feedback on this, and other people’s solutions to the problem 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a fairly common problem, it probably has a name, I just don't
This is a fairly common problem to which I have yet to find an
I think this is a fairly common question: how to put my business logic
Fairly common question this, to which I have a few answers and I'm nearly
Although this is a fairly common problem, I am struggling with what the best
While analyzing our crash logs I discovered that a fairly common crash is one
I have what seems to be a fairly common inter-process communication requirement - a
Hey everyone, I am pretty sure this is a fairly common problem. So in
This should be a fairly common question, but I haven't found a straightforward answer
I'm sure this is a fairly common situation. I'm using the Spring Security Core

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.