MongoDB Computed Pattern

The Computed Pattern in NoSQL is utilized when we have data that needs to be computed repeatedly and optimise performance of our app, and is also used when data is reads exceeeds writes by significant amount.

MongoDB Computed Pattern
Photo by JJ Ying / Unsplash

The Computed Pattern in NoSQL is used to optimize the performance of our app by pre-calculating values that are frequently accessed. This is especially useful when the number of data reads exceeds the number of writes by a significant amount.

For example, if we have a list of blog posts with comments, each post has a comment count. Instead of querying the comment count for each post, we can recalculate it and store it in the blog post document. This way, we save database resources and only need to recalculate the comment count when a new comment is added or deleted. To listen for updates to the database, MongoDB offers change streams, or you can use the middleware support provided by your ODM/ORM.

This pattern is useful for saving valuable database resources when frequently accessed calculations, such as view counts and averages, are needed.