In the world of NoSQL databases, MongoDB stands out as a powerful and flexible choice for handling large volumes of data. One of MongoDB’s most versatile concepts is the data modeling patterns, which allow users to perform complex data manipulations and computations. In this blog post, we’ll dive deep into one of the most underused and intriguing aspects of the data modeling pattern: the computed pattern.
What is a data modeling pattern?
Picture this: you’ve got interconnected datasets, and you need to dig out specific information while ensuring efficiency. Enter $lookup and $search. By fusing these features, MongoDB lets you seamlessly navigate complex datasets. Whether you’re filtering, sorting, or hunting down specific text-based queries. It’s not just a new feature; it’s a game-changer, enhancing your MongoDB experience, making data manipulation a breeze, and ensuring your applications remain responsive and insightful.
Data modeling in MongoDB is a dynamic and flexible process that allows you to design your data structures to suit your application’s specific needs. MongoDB offers various data modeling patterns that cater to different use cases, from document-oriented modeling to hierarchical and relational patterns. Whether you’re building a simple web application or a complex data-driven system, MongoDB’s data modeling patterns empower you to create scalable and efficient data structures that evolve with your application’s requirements. Explore these patterns to design databases that seamlessly integrate with your application’s logic and provide the foundation for robust, high-performance data management.
To see the full list of data patterns, check MongoDB Summary of data pattern
Why Do I Need It?
One of the main characteristics of MongoDB is the high-speed data processing, it is natural to assume that because MongoDB can ingest data without a flinch and to get the data in the form that you require can be a task easy to implement, this can cause a degradation of the cluster performance on the retrieval step such as bottlenecks and spikes of resources consumption.
Okay, what is a computed pattern then?
We can leverage on top of the concept of precalculating and caching a response, in the sense that a document will already have the data that we need.
If this sounds very similar to a traditional view, you are correct, while a view runs a query or an aggregation inside, the computed pattern allows us to store a complex result and call it every single time with close to no consumption of resources. Let’s dive in with an example.
Example: Calculating Average Product Prices
To illustrate the power of the computed pattern, let’s consider a scenario where we have a MongoDB collection containing product data, and we want to calculate the average price of products in each category. We can achieve this using the aggregation framework.

