Skip to content

Commit 71c11d5

Browse files
author
Dave
authored
DOCSP-21253 views page needs examples v5.3 (#797)
* DOCSP-21253 views page needs examples * Review feedback * Review feedback * Join example * Join example * Join example
1 parent fde707c commit 71c11d5

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

source/core/views.txt

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,223 @@ restrictions mentioned in this page:
153153
| :method:`db.collection.estimatedDocumentCount()`
154154
| :method:`db.collection.count()`
155155
| :method:`db.collection.distinct()`
156+
157+
Examples
158+
--------
159+
160+
Create the ``students`` collection to use in the following examples:
161+
162+
.. _ex-views-create-sample:
163+
164+
.. code-block:: javascript
165+
166+
db.students.insertMany( [
167+
{ sID: 22001, name: "Alex", year: 1, score: 4.0 },
168+
{ sID: 21001, name: "bernie", year: 2, score: 3.7 },
169+
{ sID: 20010, name: "Chris", year: 3, score: 2.5 },
170+
{ sID: 22021, name: "Drew", year: 1, score: 3.2 },
171+
{ sID: 17301, name: "harley", year: 6, score: 3.1 },
172+
{ sID: 21022, name: "Farmer", year: 1, score: 2.2 },
173+
{ sID: 20020, name: "george", year: 3, score: 2.8 },
174+
{ sID: 18020, name: "Harley", year: 5, score: 2.8 },
175+
] )
176+
177+
Use db.createView() to Create a View
178+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179+
180+
Use :method:`db.createView()` to create a view that is limited to first
181+
year students:
182+
183+
.. code-block:: javascript
184+
185+
db.createView(
186+
"firstYears",
187+
"students",
188+
[ { $match: { year: 1 } } ]
189+
)
190+
191+
In the example:
192+
193+
- ``firstYears`` is the name of the new view.
194+
- ``students`` is the collection the view is based on.
195+
- :pipeline:`$match` is an aggregation expression that matches first
196+
year students in the ``students`` collection.
197+
198+
This example queries the view:
199+
200+
.. code-block:: javascript
201+
202+
db.firstYears.find({}, { _id: 0 } )
203+
204+
The following output only contains the documents with data on first
205+
year students. The ``{ _id: 0 }`` :ref:`projection
206+
<method-find-projection>` suppresses the ``_id`` field in the output.
207+
208+
.. code-block:: javascript
209+
:copyable: false
210+
211+
[
212+
{ sID: 22001, name: 'Alex', year: 1, score: 4 },
213+
{ sID: 22021, name: 'Drew', year: 1, score: 3.2 },
214+
{ sID: 21022, name: 'Farmer', year: 1, score: 2.2 }
215+
]
216+
217+
Use db.createCollection() to Create a View
218+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219+
220+
The :method:`db.createCollection()` method allows you to create a
221+
collection or a view with specific options.
222+
223+
The following example creates a ``graduateStudents`` view. The view
224+
only contains documents selected by the :pipeline:`$match` stage. The
225+
optional :ref:`collation <collation>` setting determines the sort
226+
order.
227+
228+
.. code-block:: javascript
229+
230+
db.createCollection(
231+
"graduateStudents",
232+
{
233+
viewOn: "students",
234+
pipeline: [ { $match: { $expr: { $gt: [ "$year", 4 ] } } } ],
235+
collation: { locale: "en", caseFirst: "upper" }
236+
}
237+
)
238+
239+
The following example queries the view. The :pipeline:`$unset` stage
240+
removes the ``_id`` field from the output for clarity.
241+
242+
.. code-block:: javascript
243+
244+
db.graduateStudents.aggregate(
245+
[
246+
{ $sort: { name: 1 } },
247+
{ $unset: [ "_id" ] }
248+
]
249+
)
250+
251+
When the output is sorted, the :pipeline:`$sort` stage uses the
252+
:ref:`collation <collation>` ordering to sort uppercase letters before
253+
lowercase letters.
254+
255+
.. code-block:: javascript
256+
:copyable: false
257+
258+
[
259+
{ sID: 18020, name: 'Harley', year: 5, score: 2.8 },
260+
{ sID: 17301, name: 'harley', year: 6, score: 3.1 }
261+
]
262+
263+
Use a View to Join Two Collections
264+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
265+
266+
It is often convenient to use :pipeline:`$lookup` to create a view over
267+
two collections and then run queries against the view. Applications can
268+
query the view without having to construct or maintain complex
269+
pipelines.
270+
271+
Create two sample collections, ``inventory`` and ``orders``:
272+
273+
.. code-block:: javascript
274+
275+
db.inventory.insertMany( [
276+
{ prodId: 100, price: 20, quantity: 125 },
277+
{ prodId: 101, price: 10, quantity: 234 },
278+
{ prodId: 102, price: 15, quantity: 432 },
279+
{ prodId: 103, price: 17, quantity: 320 }
280+
] )
281+
282+
db.orders.insertMany( [
283+
{ orderID: 201, custid: 301, prodId: 100, numPurchased: 20 },
284+
{ orderID: 202, custid: 302, prodId: 101, numPurchased: 10 },
285+
{ orderID: 203, custid: 303, prodId: 102, numPurchased: 5 },
286+
{ orderID: 204, custid: 303, prodId: 103, numPurchased: 15 },
287+
{ orderID: 205, custid: 303, prodId: 103, numPurchased: 20 },
288+
{ orderID: 206, custid: 302, prodId: 102, numPurchased: 1 },
289+
{ orderID: 207, custid: 302, prodId: 101, numPurchased: 5 },
290+
{ orderID: 208, custid: 301, prodId: 100, numPurchased: 10 },
291+
{ orderID: 209, custid: 303, prodId: 103, numPurchased: 30 }
292+
] )
293+
294+
Create a view that combines elements from each collection:
295+
296+
.. code-block:: javascript
297+
298+
db.createView( "sales", "orders", [
299+
{
300+
$lookup:
301+
{
302+
from: "inventory",
303+
localField: "prodId",
304+
foreignField: "prodId",
305+
as: "inventoryDocs"
306+
}
307+
},
308+
{
309+
$project:
310+
{
311+
_id: 0,
312+
prodId: 1,
313+
orderId: 1,
314+
numPurchased: 1,
315+
price: "$inventoryDocs.price"
316+
}
317+
},
318+
{ $unwind: "$price" }
319+
] )
320+
321+
In the example:
322+
323+
- :method:`db.createView()` creates the ``sales`` view.
324+
- The ``sales`` view is based on the ``orders`` collection.
325+
- The :pipeline:`$lookup` stage uses the ``prodId`` field in the
326+
``orders`` collection to "join" documents in the ``inventory``
327+
collection that have matching ``prodId`` fields.
328+
- The matching documents are added as an array in the ``inventoryDocs``
329+
field.
330+
- The :pipeline:`$project` stage selects a subset of the available
331+
fields.
332+
- The :pipeline:`$unwind` stage converts the ``price`` field from an
333+
array to a scalar value.
334+
335+
The documents in the ``sales`` view are:
336+
337+
.. code-block:: javascript
338+
:copyable: false
339+
340+
{ prodId: 100, numPurchased: 20, price: 20 },
341+
{ prodId: 101, numPurchased: 10, price: 10 },
342+
{ prodId: 102, numPurchased: 5, price: 15 },
343+
{ prodId: 103, numPurchased: 15, price: 17 },
344+
{ prodId: 103, numPurchased: 20, price: 17 },
345+
{ prodId: 102, numPurchased: 1, price: 15 },
346+
{ prodId: 101, numPurchased: 5, price: 10 },
347+
{ prodId: 100, numPurchased: 10, price: 20 },
348+
{ prodId: 103, numPurchased: 30, price: 17 }
349+
350+
To find the total amount sold of each product, query the view:
351+
352+
.. code-block:: javascript
353+
354+
db.sales.aggregate( [
355+
{
356+
$group:
357+
{
358+
_id: "$prodId",
359+
amountSold: { $sum: { $multiply: [ "$price", "$numPurchased" ] } }
360+
}
361+
}
362+
] )
363+
364+
The output is:
365+
366+
.. code-block:: javascript
367+
:copyable: false
368+
369+
[
370+
{ _id: 100, amountSold: 600 },
371+
{ _id: 103, amountSold: 1105 },
372+
{ _id: 101, amountSold: 150 },
373+
{ _id: 102, amountSold: 90 }
374+
]
375+

0 commit comments

Comments
 (0)