Skip to content

Commit d720032

Browse files
Added last accumulator info alongside first (#2263)
1 parent fbc9a03 commit d720032

File tree

2 files changed

+33
-39
lines changed

2 files changed

+33
-39
lines changed

source/core/aggregation-pipeline-optimization.txt

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ include the :method:`explain <db.collection.aggregate()>` option in the
2323

2424
In addition to learning about the aggregation pipeline optimizations
2525
performed during the optimization phase, you will also see how to
26-
improve aggregation pipeline performance using indexes and document
27-
filters. See
28-
:ref:`aggregation-pipeline-optimization-indexes-and-filters`.
26+
improve aggregation pipeline performance using :ref:`indexes and
27+
document filters <aggregation-pipeline-optimization-indexes-and-filters>`.
2928

3029
.. _aggregation-pipeline-projection-optimization:
3130

@@ -34,8 +33,8 @@ Projection Optimization
3433

3534
The aggregation pipeline can determine if it requires only a subset of
3635
the fields in the documents to obtain the results. If so, the pipeline
37-
will only use those required fields, reducing the amount of data
38-
passing through the pipeline.
36+
only uses those fields, reducing the amount of data passing through the
37+
pipeline.
3938

4039
.. _aggregation-pipeline-sequence-optimization:
4140

@@ -488,15 +487,15 @@ For example, a pipeline that consists of :pipeline:`$match`,
488487
:pipeline:`$sort`, :pipeline:`$group` can benefit from indexes at
489488
every stage:
490489

491-
- An index on the :pipeline:`$match` query field can efficiently
492-
identify the relevant data
490+
- An index on the ``$match`` query field efficiently
491+
identifies the relevant data
493492

494-
- An index on the sorting field can return data in sorted order for the
495-
:pipeline:`$sort` stage
493+
- An index on the sorting field returns data in sorted order for the
494+
``$sort`` stage
496495

497-
- An index on the grouping field that matches the :pipeline:`$sort`
498-
order can return all of the field values needed to execute the
499-
:pipeline:`$group` stage (a covered query)
496+
- An index on the grouping field that matches the ``$sort``
497+
order returns all of the field values needed for the
498+
``$group`` stage, making it a covered query.
500499

501500
To determine whether a pipeline uses indexes, review the query plan and
502501
look for ``IXSCAN`` or ``DISTINCT_SCAN`` plans.
@@ -511,34 +510,29 @@ look for ``IXSCAN`` or ``DISTINCT_SCAN`` plans.
511510
For early stages in your aggregation pipeline, consider indexing the
512511
query fields. Stages that can benefit from indexes are:
513512

514-
``$match`` stage
515-
:pipeline:`$match` can use an index to filter documents if it is the
516-
first stage in the pipeline, after any optimizations from the
517-
:ref:`query planner <query-plans-query-optimization>`.
513+
:pipeline:`$match` stage
514+
During the ``$match`` stage, the server can use an index if ``$match`` is the first stage in the pipeline, after any optimizations from the :ref:`query planner <query-plans-query-optimization>`.
518515

519-
``$sort`` stage
520-
:pipeline:`$sort` can benefit from an index as long as it is not
521-
preceded by a :pipeline:`$project`, :pipeline:`$unwind`, or
516+
:pipeline:`$sort` stage
517+
During the ``$sort`` stage, the server can use an index if the stage is not preceded by a :pipeline:`$project`, :pipeline:`$unwind`, or
522518
:pipeline:`$group` stage.
523519

524-
``$group`` stage
525-
:pipeline:`$group` can use an index to find the first document in
526-
each group if it meets all of the following conditions:
527-
528-
- a :pipeline:`$sort` stage sorts the grouping field before
529-
:pipeline:`$group`
520+
:pipeline:`$group` stage
521+
During the ``$group`` stage, the server can use an index to quickly
522+
find the :group:`$first <$first>` or :group:`$last <$last>` document
523+
in each group if the stage meets both of these conditions:
530524

531-
- an index exists that matches the sort order on the grouped field
525+
- The pipeline :pipeline:`sorts <$sort>` and :pipeline:`groups
526+
<$group>` by the same field.
532527

533-
- :group:`$first` is the only accumulator in the :pipeline:`$group`
534-
stage
528+
- The ``$group`` stage only uses the :group:`$first` or
529+
:group:`$last` accumulator operator.
535530

536-
See :ref:`$group Performance Optimizations <group-pipeline-optimization>`
537-
for an example.
531+
See :ref:`$group Performance Optimizations <group-pipeline-optimization>` for an example.
538532

539-
``$geoNear`` stage
540-
:pipeline:`$geoNear` always uses an index, since it must be the first
541-
stage in a pipeline and requires a :ref:`geospatial index <index-feature-geospatial>`.
533+
:pipeline:`$geoNear` stage
534+
The server always uses an index for the ``$geoNear`` stage, since it
535+
requires a :ref:`geospatial index <index-feature-geospatial>`.
542536

543537
Additionally, stages later in the pipeline that retrieve data from
544538
other, unmodified collections can use indexes on those collections

source/reference/operator/aggregation/group.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ This section describes optimizations to improve the performance of
106106
:pipeline:`$group`. There are optimizations that you can make manually
107107
and optimizations MongoDB makes internally.
108108

109-
Optimization to Return the First Document of Each Group
110-
```````````````````````````````````````````````````````
109+
Optimization to Return the First or Last Document of Each Group
110+
```````````````````````````````````````````````````````````````
111111

112112
If a pipeline :pipeline:`sorts <$sort>` and :pipeline:`groups <$group>`
113-
by the same field and the :pipeline:`$group` stage only uses the
114-
:group:`$first` accumulator operator, consider adding an :ref:`index
113+
by the same field and the ``$group`` stage only uses the :group:`$first`
114+
or :group:`$last` accumulator operator, consider adding an :ref:`index
115115
<indexes>` on the grouped field which matches the sort order. In some
116-
cases, the :pipeline:`$group` stage can use the index to quickly find
117-
the first document of each group.
116+
cases, the ``$group`` stage can use the index to quickly find the first
117+
or last document of each group.
118118

119119
.. example::
120120

0 commit comments

Comments
 (0)