@@ -4,6 +4,12 @@ On-Demand Materialized Views
44
55.. default-domain:: mongodb
66
7+ .. contents:: On this page
8+ :local:
9+ :backlinks: none
10+ :depth: 1
11+ :class: singlecol
12+
713.. meta::
814 :description: MongoDB on-demand materialized view
915 :keywords: MongoDB, materialized view
@@ -20,6 +26,86 @@ of completely replacing the collection. This functionality allows users
2026to create on-demand materialized views, where the content of the output
2127collection can be updated each time the pipeline is run.
2228
29+
30+ .. _create-view-atlas:
31+
32+ Create a Materialized View in the {+atlas+} UI
33+ -------------------------------------------------------
34+
35+ The example in this section uses the :atlas:`sample movies dataset
36+ </sample-data/sample-mflix/>`. To learn how to load the sample dataset
37+ into your {+atlas+} deployment, see :atlas:`Load Sample Data
38+ </sample-data/#std-label-load-sample-data>`.
39+
40+ To create a materialized view in the {+atlas+} UI, follow these
41+ steps:
42+
43+ .. procedure::
44+ :style: normal
45+
46+ .. step:: Navigate to the collection
47+
48+ a. In the {+atlas+} UI, click :guilabel:`Database` in the sidebar.
49+ #. For the database deployment that contains the sample data,
50+ click :guilabel:`Browse Collections`.
51+ #. In the left navigation pane, select the
52+ :guilabel:`sample_training` database.
53+ #. Select the :guilabel:`grades` collection.
54+
55+ .. step:: Click the :guilabel:`Aggregation` tab
56+
57+ .. step:: Click :guilabel:`Add Stage`
58+
59+ .. step:: Select an aggregation stage from the :guilabel:`Select` drop-down menu
60+
61+ The aggregation stage transforms the data that you want to save as
62+ a view. To learn more about available aggregation stages, see
63+ :ref:`agg-pipeline-quick-ref`.
64+
65+ For this example, add a new field with the :pipeline:`$set` stage:
66+
67+ a. Select :pipeline:`$set` from the :guilabel:`Select` drop-down
68+ menu.
69+ #. Add the following syntax to the aggregation pipeline
70+ editor to create an average score across all ``score``
71+ values in the ``scores`` array within the ``grades``
72+ collection:
73+
74+ .. code-block::
75+
76+ {
77+ averageScore: { $avg: "$scores.score" }
78+ }
79+
80+ {+atlas+} adds the ``averageScore`` value to each document.
81+
82+ .. step:: Click :guilabel:`Add Stage`
83+
84+ .. step:: Add the ``$out`` stage
85+
86+ a. Select the :pipeline:`$out` stage from the :guilabel:`Select`
87+ drop-down menu.
88+ #. Add the following syntax to the aggregation pipeline
89+ to write the results of the pipeline to the ``myView``
90+ collection in the ``sample_training`` database:
91+
92+ .. code-block::
93+
94+ 'myView'
95+
96+ #. Click :guilabel:`Save Documents`.
97+
98+ The :pipeline:`$out` stage writes the results of the
99+ aggregation pipeline to the specified collection, which creates
100+ the view. To learn more, see :pipeline:`$out`.
101+
102+ Refresh the list of collections to see the ``myView`` collection.
103+
104+ To learn how to query the ``myView`` collection in the {+atlas+}
105+ UI, see :atlas:`View, Filter, and Sort Documents
106+ </atlas-ui/documents/#view--filter--and-sort-documents>` in the
107+ {+atlas+} documentation.
108+
23109Example
24110-------
25111
0 commit comments