@@ -39,6 +39,7 @@ aggregation builder to create the stages of an aggregation pipeline:
39
39
40
40
- :ref:`laravel-add-aggregation-dependency`
41
41
- :ref:`laravel-build-aggregation`
42
+ - :ref:`laravel-aggregation-examples`
42
43
- :ref:`laravel-create-custom-operator-factory`
43
44
44
45
.. tip::
@@ -71,12 +72,13 @@ includes the following line in the ``require`` object:
71
72
72
73
.. _laravel-build-aggregation:
73
74
74
- Create an Aggregation Pipeline
75
- ------------------------------
75
+ Create Aggregation Stages
76
+ -------------------------
76
77
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.
80
82
81
83
The aggregation builder includes the following namespaces that you can import
82
84
to build aggregation stages:
@@ -92,13 +94,12 @@ to build aggregation stages:
92
94
GitHub repository.
93
95
94
96
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:
96
98
97
99
- :ref:`laravel-aggregation-match-stage-example`
98
100
- :ref:`laravel-aggregation-group-stage-example`
99
101
- :ref:`laravel-aggregation-sort-stage-example`
100
102
- :ref:`laravel-aggregation-project-stage-example`
101
- - :ref:`laravel-aggregation-pipeline-example`
102
103
103
104
To learn more about MongoDB aggregation operators, see
104
105
: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
112
113
``insert()`` method:
113
114
114
115
.. 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
119
120
120
121
.. _laravel-aggregation-match-stage-example:
121
122
@@ -151,6 +152,7 @@ Click the :guilabel:`{+code-output-label+}` button to see the documents
151
152
returned by running the code:
152
153
153
154
.. io-code-block::
155
+ :copyable: true
154
156
155
157
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
156
158
:language: php
@@ -226,6 +228,7 @@ Click the :guilabel:`{+code-output-label+}` button to see the documents
226
228
returned by running the code:
227
229
228
230
.. io-code-block::
231
+ :copyable: true
229
232
230
233
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
231
234
:language: php
@@ -270,6 +273,7 @@ alphabetical order. Click the :guilabel:`{+code-output-label+}` button to see
270
273
the documents returned by running the code:
271
274
272
275
.. io-code-block::
276
+ :copyable: true
273
277
274
278
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
275
279
:language: php
@@ -370,6 +374,7 @@ Click the :guilabel:`{+code-output-label+}` button to see the data returned by
370
374
running the code:
371
375
372
376
.. io-code-block::
377
+ :copyable: true
373
378
374
379
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
375
380
:language: php
@@ -390,56 +395,164 @@ running the code:
390
395
{ "name": "Ellis Lee" }
391
396
]
392
397
398
+ .. _laravel-aggregation-examples:
393
399
394
- .. _laravel-aggregation-pipeline-example:
400
+ Build Aggregation Pipelines
401
+ ---------------------------
395
402
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:
398
411
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`
402
415
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
412
440
413
441
Click the :guilabel:`{+code-output-label+}` button to see the data returned by
414
442
running the code:
415
443
416
444
.. io-code-block::
445
+ :copyable: true
417
446
418
447
.. input:: /includes/fundamentals/aggregation/AggregationsBuilderTest.php
419
448
:language: php
420
449
: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
423
452
424
453
.. output::
425
454
:language: json
426
455
:visible: false
427
456
428
457
[
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" } }
437
507
]
438
508
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.
440
523
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
+ ]
443
556
444
557
.. _laravel-create-custom-operator-factory:
445
558
0 commit comments