0

I have a python web application that connects to a MongoDB database. At the moment I only have a collection where I have a lot of documents. Users can edit these documents so I want to implement a history of changes. Actually I am only interested in storing the username and the edited fields (i.e. the old value and the new value). So, I was thinking of creating a new collection called "change_history" and store there all the change history, using manually the reference to the _id of the document. A record of that collection would look something like this:

{
_id: "12345"
document_id: "456d"
date_change: "2023-01-01"
username: "jane"
color: {last_field: "blue", new_field: "red"}
}

So, later on when you want to consult the changes of a document you would have to filter the collection "change_history" according to the document_id. I could even sort them by date. Is this a good approach in terms of practicality and efficiency? I don't know if there is a mongo tool for python that does this automatically. Or if I should in the other collection group them by document_id. One of the limitations I have is that I don't know yet the requirement of how users will be able to see this in the frontend.

Diego L
  • 185
  • 1
  • 9
  • Does this answer your question? [MongoDB/NoSQL: Keeping Document Change History](https://stackoverflow.com/questions/3507624/mongodb-nosql-keeping-document-change-history) – ray Jun 01 '23 at 21:46
  • I had checked this question. If nothing has changed for several years, it partially answers. I just need to know if it is better to nest them by document_id or all the history in the top level of "change_history". – Diego L Jun 01 '23 at 21:50

0 Answers0