MongoDB Computed Pattern

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.