- Introduction to MongoDB >
- Databases and Collections >
- On-Demand Materialized Views
On-Demand Materialized Views¶
Note
The following page discusses on-demand materialized views. For discussion of views, see Views instead.
Starting in version 4.2, MongoDB adds the $merge
stage for
the aggregation pipeline. This
stage can merge the pipeline results to an existing collection instead
of completely replacing the collection. This functionality allows users
to create on-demand materialized views, where the content of the output
collection can be updated each time the pipeline is run.
Example¶
Assume near the end of January 2019, the collection bakesales
contains the sales information by items:
1. Define the On-Demand Materialized View¶
The following updateMonthlySales
function defines a
monthlybakesales
materialized view that contains the cumulative
monthly sales information. In the example, the function takes a date
parameter to only update monthly sales information starting from a
particular date.
The
$match
stage filters the data to process only those sales greater than or equal to thestartDate
.The
$group
stage groups the sales information by the year-month. The documents output by this stage have the form:The
$merge
stage writes the output to themonthlybakesales
collection.Based on the
_id
field (the default for unsharded output collections), the stage checks if the document in the aggregation results matches an existing document in the collection:- When there is a match (i.e. a document with the same year-month already exists in the collection), the stage replaces the existing document with the document from the aggregation results.
- When there is not a match, the stage inserts the document from the aggregation results into the collection (the default behavior when not matched).
2. Perform Initial Run¶
For the initial run, you can pass in a date of new
ISODate("1970-01-01")
:
After the initial run, the monthlybakesales
contains the following
documents; i.e. db.monthlybakesales.find().sort( { _id: 1 } )
returns the following:
3. Refresh Materialized View¶
Assume that by the first week in February 2019, the bakesales
collection is updated with newer sales information; specifically,
additional January and February sales.
To refresh the monthlybakesales
data for January and February, run
the function again to rerun the aggregation pipeline, starting with
new ISODate("2019-01-01")
.
The content of monthlybakesales
has been updated to reflect the
most recent data in the bakesales
collection; i.e.
db.monthlybakesales.find().sort( { _id: 1 } )
returns the following:
Additional Information¶
The $merge
stage:
- Can output to a collection in the same or different database.
- Creates a new collection if the output collection does not already exist.
- Can incorporate results (insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline) into an existing collection.
- Can output to a sharded collection. Input collection can also be sharded.
See $merge
for:
- More information on
$merge
and available options - Example: On-Demand Materialized View: Initial Creation
- Example: On-Demand Materialized View: Update/Replace Data
- Example: Only Insert New Data