Skip to content

Commit dbb905f

Browse files
committed
DOCSP-44554: add more agg exs
1 parent 3e5ba46 commit dbb905f

File tree

5 files changed

+411
-53
lines changed

5 files changed

+411
-53
lines changed

docs/fundamentals/aggregation-builder.txt

Lines changed: 152 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ aggregation builder to create the stages of an aggregation pipeline:
3939

4040
- :ref:`laravel-add-aggregation-dependency`
4141
- :ref:`laravel-build-aggregation`
42+
- :ref:`laravel-aggregation-examples`
4243
- :ref:`laravel-create-custom-operator-factory`
4344

4445
.. tip::
@@ -71,12 +72,13 @@ includes the following line in the ``require`` object:
7172

7273
.. _laravel-build-aggregation:
7374

74-
Create an Aggregation Pipeline
75-
------------------------------
75+
Create Aggregation Stages
76+
-------------------------
7677

77-
To start an aggregation pipeline, call the ``Model::aggregate()`` method.
78-
Then, chain the aggregation stage methods in the sequence you want them to
79-
run.
78+
To start an aggregation stage, call the ``Model::aggregate()`` method.
79+
Then, chain an aggregation stage method and specify the necessary
80+
parameters for the stage. For example, you can call the ``sort()``
81+
operator method to build a ``$sort`` stage.
8082

8183
The aggregation builder includes the following namespaces that you can import
8284
to build aggregation stages:
@@ -92,13 +94,12 @@ to build aggregation stages:
9294
GitHub repository.
9395

9496
This section features the following examples, which show how to use common
95-
aggregation stages and combine stages to build an aggregation pipeline:
97+
aggregation stages:
9698

9799
- :ref:`laravel-aggregation-match-stage-example`
98100
- :ref:`laravel-aggregation-group-stage-example`
99101
- :ref:`laravel-aggregation-sort-stage-example`
100102
- :ref:`laravel-aggregation-project-stage-example`
101-
- :ref:`laravel-aggregation-pipeline-example`
102103

103104
To learn more about MongoDB aggregation operators, see
104105
:manual:`Aggregation Stages </reference/operator/aggregation-pipeline/>` in
@@ -112,10 +113,10 @@ by the ``User`` model. You can add the sample data by running the following
112113
``insert()`` method:
113114

114115
.. literalinclude:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
115-
:language: php
116-
:dedent:
117-
:start-after: begin aggregation builder sample data
118-
:end-before: end aggregation builder sample data
116+
:language: php
117+
:dedent:
118+
:start-after: begin aggregation builder sample data
119+
:end-before: end aggregation builder sample data
119120

120121
.. _laravel-aggregation-match-stage-example:
121122

@@ -151,6 +152,7 @@ Click the :guilabel:`{+code-output-label+}` button to see the documents
151152
returned by running the code:
152153

153154
.. io-code-block::
155+
:copyable: true
154156

155157
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
156158
:language: php
@@ -226,6 +228,7 @@ Click the :guilabel:`{+code-output-label+}` button to see the documents
226228
returned by running the code:
227229

228230
.. io-code-block::
231+
:copyable: true
229232

230233
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
231234
:language: php
@@ -270,6 +273,7 @@ alphabetical order. Click the :guilabel:`{+code-output-label+}` button to see
270273
the documents returned by running the code:
271274

272275
.. io-code-block::
276+
:copyable: true
273277

274278
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
275279
:language: php
@@ -370,6 +374,7 @@ Click the :guilabel:`{+code-output-label+}` button to see the data returned by
370374
running the code:
371375

372376
.. io-code-block::
377+
:copyable: true
373378

374379
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
375380
:language: php
@@ -390,56 +395,164 @@ running the code:
390395
{ "name": "Ellis Lee" }
391396
]
392397

398+
.. _laravel-aggregation-examples:
393399

394-
.. _laravel-aggregation-pipeline-example:
400+
Build Aggregation Pipelines
401+
---------------------------
395402

396-
Aggregation Pipeline Example
397-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
403+
To build an aggregation pipeline, call the ``Model::aggregate()`` method,
404+
then chain the aggregation stages in the sequence you want them to
405+
run. The examples in this section are adapted from the {+server-docs-name+}.
406+
Each example provides a link to the sample data that you can insert into
407+
your database to test the aggregation operation.
408+
409+
This section features the following examples, which show how to use common
410+
aggregation stages:
398411

399-
This aggregation pipeline example chains multiple stages. Each stage runs
400-
on the output retrieved from each preceding stage. In this example, the
401-
stages perform the following operations sequentially:
412+
- :ref:`laravel-aggregation-filter-group-example`
413+
- :ref:`laravel-aggregation-unwind-example`
414+
- :ref:`laravel-aggregation-lookup-example`
402415

403-
- Add the ``birth_year`` field to the documents and set the value to the year
404-
extracted from the ``birthday`` field.
405-
- Group the documents by the value of the ``occupation`` field and compute
406-
the average value of ``birth_year`` for each group by using the
407-
``Accumulator::avg()`` function. Assign the result of the computation to
408-
the ``birth_year_avg`` field.
409-
- Sort the documents by the group key field in ascending order.
410-
- Create the ``profession`` field from the value of the group key field,
411-
include the ``birth_year_avg`` field, and omit the ``_id`` field.
416+
.. _laravel-aggregation-filter-group-example:
417+
418+
Filter and Group Example
419+
~~~~~~~~~~~~~~~~~~~~~~~~
420+
421+
This example uses the sample data given in the :manual:`Calculate Count,
422+
Sum, and Average </reference/operator/aggregation/group/#calculate-count--sum--and-average>`
423+
section of the ``$group`` stage reference in the {+server-docs-name+}.
424+
425+
The following code example calculates the total sales amount, average
426+
sales quantity, and sale count for each day in the year 2014. To do so,
427+
it uses an aggregation pipeline that contains the following stages:
428+
429+
1. :manual:`$match </reference/operator/aggregation/match/>` stage to
430+
filter for documents that contain a ``date`` field in which the year is
431+
2014
432+
433+
#. :manual:`$group </reference/operator/aggregation/group/>` stage to
434+
group the documents by date and calculate the total sale amount,
435+
average quantity, and total count for each group
436+
437+
#. :manual:`$sort </reference/operator/aggregation/sort/>` stage to
438+
sort the results by the total sale amount for each group in descending
439+
order
412440

413441
Click the :guilabel:`{+code-output-label+}` button to see the data returned by
414442
running the code:
415443

416444
.. io-code-block::
445+
:copyable: true
417446

418447
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
419448
:language: php
420449
:dedent:
421-
:start-after: begin pipeline example
422-
:end-before: end pipeline example
450+
:start-after: start-builder-match-group
451+
:end-before: end-builder-match-group
423452

424453
.. output::
425454
:language: json
426455
:visible: false
427456

428457
[
429-
{
430-
"birth_year_avg": 1991.5,
431-
"profession": "designer"
432-
},
433-
{
434-
"birth_year_avg": 1995.5,
435-
"profession": "engineer"
436-
}
458+
{ "_id": "2014-04-04", "totalSaleAmount": { "$numberDecimal": "200" }, "averageQuantity": 15, "count": 2 },
459+
{ "_id": "2014-03-15", "totalSaleAmount": { "$numberDecimal": "50" }, "averageQuantity": 10, "count": 1 },
460+
{ "_id": "2014-03-01", "totalSaleAmount": { "$numberDecimal": "40" }, "averageQuantity": 1.5, "count": 2 }
461+
]
462+
463+
.. _laravel-aggregation-unwind-example:
464+
465+
Unwind Embedded Arrays Example
466+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
467+
468+
This example uses the sample data given in the :manual:`Unwind Embedded Arrays
469+
</reference/operator/aggregation/unwind/#unwind-embedded-arrays>`
470+
section of the ``$unwind`` stage reference in the {+server-docs-name+}.
471+
472+
The following code example groups sold items by their tags and
473+
calculates the total sales amount for each tag. To do so,
474+
it uses an aggregation pipeline that contains the following stages:
475+
476+
1. :manual:`$unwind </reference/operator/aggregation/unwind/>` stage to
477+
output a separate document for each element in the ``items`` array
478+
479+
#. :manual:`$unwind </reference/operator/aggregation/unwind/>` stage to
480+
output a separate document for each element in the ``items.tags`` arrays
481+
482+
#. :manual:`$group </reference/operator/aggregation/group/>` stage to
483+
group the documents by the tag value and calculates the total sales
484+
amount of items that have each tag
485+
486+
Click the :guilabel:`{+code-output-label+}` button to see the data returned by
487+
running the code:
488+
489+
.. io-code-block::
490+
:copyable: true
491+
492+
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
493+
:start-after: start-builder-unwind
494+
:end-before: end-builder-unwind
495+
:language: php
496+
:dedent:
497+
498+
.. output::
499+
:visible: false
500+
501+
[
502+
{ "_id": "school", "totalSalesAmount": { "$numberDecimal": "104.85" } },
503+
{ "_id": "electronics", "totalSalesAmount": { "$numberDecimal": "800.00" } },
504+
{ "_id": "writing", "totalSalesAmount": { "$numberDecimal": "60.00" } },
505+
{ "_id": "office", "totalSalesAmount": { "$numberDecimal": "1019.60" } },
506+
{ "_id": "stationary", "totalSalesAmount": { "$numberDecimal": "264.45" } }
437507
]
438508

439-
.. note::
509+
.. _laravel-aggregation-lookup-example:
510+
511+
Single Equality Join Example
512+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
513+
514+
This example uses the sample data given in the :manual:`Perform a Single
515+
Equality Join with $lookup
516+
</reference/operator/aggregation/lookup/#perform-a-single-equality-join-with--lookup>`
517+
section of the ``$lookup`` stage reference in the {+server-docs-name+}.
518+
519+
The following code example joins the documents from the ``orders``
520+
collection with the documents from the ``inventory`` collection by using
521+
the ``item`` field from the ``orders`` collection and the ``sku`` field
522+
from the ``inventory`` collection.
440523

441-
Since this pipeline omits the ``match()`` stage, the input for the initial
442-
stage consists of all the documents in the collection.
524+
To do so, the example uses an aggregation pipeline that contains a
525+
:manual:`$lookup </reference/operator/aggregation/lookup/>` stage that
526+
specifies the collection to retrieve data from and the local and
527+
foreign field names.
528+
529+
Click the :guilabel:`{+code-output-label+}` button to see the data returned by
530+
running the code:
531+
532+
.. io-code-block::
533+
:copyable: true
534+
535+
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
536+
:start-after: start-builder-lookup
537+
:end-before: end-builder-lookup
538+
:language: php
539+
:dedent:
540+
541+
.. output::
542+
:visible: false
543+
544+
[
545+
{ "_id": 1, "item": "almonds", "price": 12, "quantity": 2, "inventory_docs": [
546+
{ "_id": 1, "sku": "almonds", "description": "product 1", "instock": 120 }
547+
] },
548+
{ "_id": 2, "item": "pecans", "price": 20, "quantity": 1, "inventory_docs": [
549+
{ "_id": 4, "sku": "pecans", "description": "product 4", "instock": 70 }
550+
] },
551+
{ "_id": 3, "inventory_docs": [
552+
{ "_id": 5, "sku": null, "description": "Incomplete" },
553+
{ "_id": 6 }
554+
] }
555+
]
443556

444557
.. _laravel-create-custom-operator-factory:
445558

0 commit comments

Comments
 (0)