-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
views.txt
144 lines (106 loc) · 3.69 KB
/
views.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
.. _views-landing-page:
=====
Views
=====
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
A MongoDB view is a queryable object whose contents are defined by an
:ref:`aggregation pipeline <aggregation-pipeline>` on other collections
or views. MongoDB does not persist the view contents to disk. A view's
content is computed on-demand when a client :ref:`queries
<views-supported-operations>` the view. MongoDB can require clients to
:ref:`have permission <authorization>` to query the view. MongoDB does
not support write operations against views.
For example, you can:
- Create a view on a collection of employee data to
:pipeline:`exclude <$project>` any private or personal
information (PII). Applications can query the view for employee
data that does not contain any PII.
- Create a view on a collection of collected sensor data to
:pipeline:`add <$addFields>` computed fields and metrics. Applications
can use simple find operations to query the data.
- Create a view that :pipeline:`joins <$lookup>` two collections
containing inventory and order history respectively. Applications can
query the joined data without managing or understanding the underlying
complex pipeline.
When clients :ref:`query a view <views-supported-operations>`, MongoDB
appends the client query to the underlying pipeline and returns the
results of that combined pipeline to the client. MongoDB may apply
:doc:`aggregation pipeline optimizations
</core/aggregation-pipeline-optimization>` to the combined pipeline.
Create View
-----------
To create or define a view:
- Use the :method:`db.createCollection()` method or the
:dbcommand:`create` command:
.. code-block:: javascript
db.createCollection(
"<viewName>",
{
"viewOn" : "<source>",
"pipeline" : [<pipeline>],
"collation" : { <collation> }
}
)
- Use the :method:`db.createView()` method:
.. code-block:: javascript
db.createView(
"<viewName>",
"<source>",
[<pipeline>],
{
"collation" : { <collation> }
}
)
.. note::
You must create views in the same database as the source collection.
Behavior
--------
.. include:: /includes/extracts/views-behavior.rst
Public View Definition
~~~~~~~~~~~~~~~~~~~~~~
Operations that lists collections, such as
:method:`db.getCollectionInfos()` and
:method:`db.getCollectionNames()`, include views in their outputs.
.. important::
.. include:: /includes/extracts/views-public-definition.rst
Drop a View
-----------
To remove a view, use the :method:`db.collection.drop()` method on the
view.
Modify a View
-------------
You can modify a view either by dropping and recreating the view or
using the :dbcommand:`collMod` command.
.. _views-supported-operations:
Supported Operations
--------------------
The following operations provide support for views, except for the
restrictions mentioned in this page:
.. list-table::
:header-rows: 1
* - Commands
- Methods
* - :dbcommand:`create`
- | :method:`db.createCollection()`
| :method:`db.createView()`
* - :dbcommand:`collMod`
-
* -
- | :method:`db.getCollection()`
| :method:`db.getCollectionInfos()`
| :method:`db.getCollectionNames()`
* - | :dbcommand:`find`
| :dbcommand:`distinct`
| :dbcommand:`count`
- | :method:`db.collection.aggregate()`
| :method:`db.collection.find()`
| :method:`db.collection.findOne()`
| :method:`db.collection.countDocuments()`
| :method:`db.collection.estimatedDocumentCount()`
| :method:`db.collection.count()`
| :method:`db.collection.distinct()`