Understanding Materialized Views in MongoDB (through Examples)

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

Materialized views are a powerful concept in database management, allowing you to store the result of a query in a persistent form. This concept is particularly useful in MongoDB in the form of pre-aggregated, managed collections. These views can improve read performance on complicated queries, at the cost of additional storage and maintenance overhead. In this tutorial, we’ll dive deep into understanding materialized views in MongoDB and show you how to implement them through a series of examples.

What Are Materialized Views?

In the realm of databases, a materialized view is essentially a pre-computed data set derived from a query’s result set, which can be stored and refreshed as needed. Traditional views in databases are not stored on disk and are recalculated each time they are accessed. Materialized views, on the other hand, are stored on disk and provide quicker read access at the cost of slower writes and the necessity of being refreshed. While MongoDB does not provide ‘materialized views’ in the traditional sense, similar functionality is easily built using MongoDB’s aggregation framework and change streams.

Basic Aggregation Pipeline and manual refresh

Let’s begin with a basic example of creating a collection that could function as a materialized view using MongoDB’s aggregation pipeline. Suppose we have a collection named orders, which holds a variety of order detail documents:

// Example document in the 'orders' collection
{
  "_id": ObjectId("some id"),
  "customerId": "someCustomerId",
  "amount": 150.00,
  "status": "shipped"
}

We want to create a materialized view to hold the total amount of orders per customer. We first define an aggregation pipeline:

db.orders.aggregate([
  {
    $group: {
      _id: "$customerId",
      totalAmount: { $sum: "$amount" }
    }
  }
]);

We can manually store the result of this aggregation in a new collection, let’s call it customerOrderTotals:

db.customerOrderTotals.insertMany([
  // Result of the aggregation pipeline goes here
]);

Creating Indexed Views

For our view to be efficient, we need to ensure that appropriate indexes are set. In this example, a common query might be to find the customer’s total order amount by their customerId, so we should index on that field:

db.customerOrderTotals.createIndex({ "_id": 1 });

This ensures that queries against our materialized view will be quick and efficient.

Maintaining the View with Change Streams

To maintain a materialized view, we need to listen for changes in the source collection and apply them to our view. MongoDB’s Change Streams allow for real-time tracking of data changes:

const changeStream = db.orders.watch();
changeStream.on('change', (change) => {
  // Logic to update 'customerOrderTotals'.
});

The above setup creates a reactive system where every change in the orders collection triggers view refresh logic.

Incrementally Updating

Instead of rebuilding the entire view each time, we can use the information given by the change stream to update only the affected data:

changeStream.on('change', (change) => {
  if (change.operationType === 'insert') {
    const order = change.fullDocument;
    db.customerOrderTotals.updateOne(
      {_id: order.customerId},
      {$inc: {totalAmount: order.amount}},
      {upsert: true}
    );
  }
  // Add cases for update and delete operations
});

This approach is more efficient than recalculating the entire view and can provide significant improvements in read performance.

Automated Refresh with Scheduled Tasks

If near real-time updates are not required, a scheduled task can be utilized to refresh the materialized view periodically:

// Using a system scheduler like cron
// Execute the view refresh logic, say, every hour
0 * * * * /usr/bin/mongo refreshMaterializedView.js

The above cron job runs a MongoDB script refreshMaterializedView.js that contains the refresh logic on an hourly basis.

Advanced: Handling Joins

MongoDB’s aggregation framework allows performing data processing equivalent to a SQL JOIN using the $lookup operator. We can include this in our materialized view to bring together data from different collections:

db.orders.aggregate([
  {
    $lookup: {
      from: "customers",
      localField: "customerId",
      foreignField: "_id",
      as: "customerDetails"
    }
  },
  ... // Further aggregation stages
]);

Note that for a materialized view with a JOIN, maintaining it becomes more complex as changes in either collection may necessitate a refresh.

Benefits and Trade-offs

Before using materialized views, it’s important to consider the trade-offs:

  • Read Performance: Materialized views can greatly improve read performance for complex aggregations.
  • Write Performance: The need to maintain the view can slow down write operations.
  • Storage: Duplicate data means additional storage overhead.
  • Complexity: The logic to maintain the view can add complexity to your application.

These trade-offs will vary depending on the database’s workload, size, and specific use cases.

Conclusion

In conclusion, materialized views, while not a native feature in MongoDB, can be effectively implemented to optimize read operations. By leveraging the aggregation framework and change streams, we can create and maintain performant, pre-aggregated collections. Whether using materialized views is the right choice depends on the specific requirements and constraints of the system.