Versioning MongoDB Documents with Spring Data - Spring Boot

In one of my project I needed to manage all history of the modification that could occurs on my MongoDB document, in order to provide audit-ability.

My Technical stack is based on Spring Boot / Spring Data and MongoDB.

My post is inpired by :



I choose to handle data integrity by using a single document to store all versions (current + previous) :


Solution :


The Abstract Document : the root document that will embed alls versions The AbstractEntity : the versionned entity The MongoListener to update audit informations. The user audit aware, in order to propagate user informations Now we can define our own Entity store in a document. For example we define a Project Entity : It will generate this kind of MongoDB document. In Json :

Limitations :

This solution does not handle the fact that a MongoDocument has a actually a limitation of 16MBytes.

The solution to detect this would be to used the Spring AbstractMongoEventListener and evaluate the size before inserting. But it will cost twice the price of converting DBObject to mongo Bson.
Another solution would be to detect and react to the insertion Exception due to memory size limitation.
The previous versions oversize may be stored in a specific collection with a custom handling.

The audit informations on the entity suppose that a document cannot be modified, but a new version is created.

Comments