@@ -13,10 +13,23 @@ Aggregation Pipeline
1313 :class: singlecol
1414
1515
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:
2033
2134.. code-block:: ruby
2235
@@ -45,22 +58,22 @@ could do the following:
4558.. code-block:: ruby
4659
4760 band_ids = Band.collection.aggregate([
48- {'$lookup' => {
61+ { '$lookup' => {
4962 from: 'tours',
5063 localField: '_id',
5164 foreignField: 'band_id',
5265 as: 'tours',
53- }},
54- {'$lookup' => {
66+ } },
67+ { '$lookup' => {
5568 from: 'awards',
5669 localField: '_id',
5770 foreignField: 'band_id',
5871 as: 'awards',
59- }},
60- {'$match' => {
72+ } },
73+ { '$match' => {
6174 'tours.year' => {'$gte' => 2000},
6275 'awards._id' => {'$exists' => true},
63- }},
76+ } },
6477 {'$project' => {_id: 1}},
6578 ])
6679 bands = Band.find(band_ids)
@@ -77,7 +90,7 @@ having to send the list of document ids to Mongoid in the second query
7790.. _aggregation-pipeline-builder-dsl:
7891
7992Builder DSL
80- -----------
93+ ===========
8194
8295Mongoid provides limited support for constructing the aggregation pipeline
8396itself using a high-level DSL. The following aggregation pipeline operators
@@ -113,7 +126,7 @@ For example, given the following models:
113126 field :name, type: String
114127 end
115128
116- We could find out which states a participant visited:
129+ We can find out which states a participant visited:
117130
118131.. code-block:: ruby
119132
0 commit comments