@@ -13,10 +13,23 @@ Aggregation Pipeline
13
13
:class: singlecol
14
14
15
15
16
- Mongoid exposes MongoDB's `aggregation pipeline
17
- <https://mongodb.com/docs/manual/core/aggregation-pipeline/>`_ for queries
18
- involving multiple referenced associations at the same time. Given the
19
- following model definitions with referenced associations:
16
+ Mongoid exposes `MongoDB's aggregation pipeline
17
+ <https://www.mongodb.com/docs/manual/core/aggregation-pipeline/>`_,
18
+ which is used to construct flows of operations that process and return results.
19
+ The aggregation pipeline is a superset of the deprecated
20
+ :ref:`map/reduce framework <map-reduce>` functionality.
21
+
22
+
23
+ Basic Usage
24
+ ===========
25
+
26
+ .. _aggregation-pipeline-example-multiple-collections:
27
+
28
+ Querying Across Multiple Collections
29
+ ````````````````````````````````````
30
+
31
+ The aggregation pipeline may be used for queries involving multiple
32
+ referenced associations at the same time:
20
33
21
34
.. code-block:: ruby
22
35
@@ -45,22 +58,22 @@ could do the following:
45
58
.. code-block:: ruby
46
59
47
60
band_ids = Band.collection.aggregate([
48
- {'$lookup' => {
61
+ { '$lookup' => {
49
62
from: 'tours',
50
63
localField: '_id',
51
64
foreignField: 'band_id',
52
65
as: 'tours',
53
- }},
54
- {'$lookup' => {
66
+ } },
67
+ { '$lookup' => {
55
68
from: 'awards',
56
69
localField: '_id',
57
70
foreignField: 'band_id',
58
71
as: 'awards',
59
- }},
60
- {'$match' => {
72
+ } },
73
+ { '$match' => {
61
74
'tours.year' => {'$gte' => 2000},
62
75
'awards._id' => {'$exists' => true},
63
- }},
76
+ } },
64
77
{'$project' => {_id: 1}},
65
78
])
66
79
bands = Band.find(band_ids)
@@ -77,7 +90,7 @@ having to send the list of document ids to Mongoid in the second query
77
90
.. _aggregation-pipeline-builder-dsl:
78
91
79
92
Builder DSL
80
- -----------
93
+ ===========
81
94
82
95
Mongoid provides limited support for constructing the aggregation pipeline
83
96
itself using a high-level DSL. The following aggregation pipeline operators
@@ -113,7 +126,7 @@ For example, given the following models:
113
126
field :name, type: String
114
127
end
115
128
116
- We could find out which states a participant visited:
129
+ We can find out which states a participant visited:
117
130
118
131
.. code-block:: ruby
119
132
0 commit comments