liveBook · Manning

Materialized view

As explained in MongoDB 8.0 in Action, Third Edition

A materialized view is a persistently stored result of a query or transformation that can be refreshed as source data changes.

In MongoDB, an on-demand materialized view is the stored result of an aggregation pipeline. The pipeline writes its result to disk with $merge or $out, so a read retrieves precalculated data instead of recalculating the pipeline. This can improve read performance, especially when the pipeline is complex or aggregates a large volume of data. section 2.6.3

The trade-off is freshness. A standard view runs its pipeline when queried and reflects the underlying data at that time; an on-demand materialized view does not update automatically. When source data changes, rerun the aggregation pipeline to write updated results to the stored collection. section 2.6.3 section 2.7

Choosing between live and stored results

A view that filters a routes collection with $match can provide a convenient live subset. If repeatedly calculating a pipeline becomes costly, storing its result as an on-demand materialized view can speed reads—but the stored result needs an explicit refresh after the routes change. section 2.6.3

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines disk-stored results and manual refresh, but defers the details of $merge and $out.
Examples
Contrasts stored results with a routes view that uses $match to filter by airline ID; gives no materialized-view command.
What it assumes
Builds on collections and aggregation pipelines while defining the stored-result and refresh behavior directly.
What you can do after
Operate/configure it by rerunning the aggregation pipeline when changes to source data must appear in stored results.
How it connects
Connects materialized views to MongoDB collections, aggregation pipelines, standard views, $merge, and $out.

Also explained in

Compare how 5 books explain materialized view →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.