Document Versioning

We are using C# driver in .netcore. We are looking for sound implementation of Document Versioning similar to SQL Server versioning. I have gone through BlogPost where they discuss document versioning.

Thanks

Document versioning in MongoDB can be implemented in multiple ways, each with its pros and cons. Here are a few options:

  1. Embed version in the Document: Store a version field in each document and increment it with each update.
  2. Store each version as a separate document: Store each version of the document as a separate document in the collection, with a unique version identifier.
  3. Use MongoDB’s built-in change streams: MongoDB 4.0 and later support change streams, which can be used to track changes to a collection in real-time.
  4. Use a separate collection for version history: Store the version history of a document in a separate collection, which can be linked to the original document using a reference.

Whichever method you choose, make sure to test it thoroughly and to consider the trade-off between the complexity of the solution and the requirements for versioning in your use case.

  1. Does the process which reads Change Stream have to be running consciously or it can have downtime? Is there the concept of “resume from” when I start reading from ChangeStream again? What are the provisions for consistency in this?

  2. I was also thinking about implementing versioning in my application itself with the below approach.

Below operations would be in the transaction

Insert for the document (newDocument) comes in with PrimaryKey = PolicytId

  • Get the CurrentVersion from the database using PolicyId
  • Assign the Version number of the newDocument as CurrentVersion + 1
  • Delete existing document and insert newDocument in collection
  • Also insert newDocument in history_collection or archive_collection

What do you think about this approach?

Hi @Marmik_Shah

There are a couple of blog posts that may be useful to you regarding this topic:

Note that those two posts are quite technical, but contains a lot of interesting information regarding the pros/cons of some approaches. Those are a bit old, but still relevant today since it deals with schema design.

Best regards
Kevin

Thanks for the links.

See