Skip to content

Commit 8a41773

Browse files
committed
DOCS-489 aggregation operators and indexes
1 parent 83932aa commit 8a41773

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

source/applications/aggregation.txt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,44 @@ Document size <limit-bson-document-size>` limit, which is currently 16 megabytes
174174
Optimizing Performance
175175
----------------------
176176

177-
Early Filtering
178-
~~~~~~~~~~~~~~~
179-
180177
Because you will always call :method:`aggregate` on a
181178
:term:`collection` object, which logically inserts the *entire* collection into
182179
the aggregation pipeline, you may want to optimize the operation
183180
by avoiding scanning the entire collection whenever possible.
184181

182+
Aggregation Operators and Indexes
183+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184+
185+
Depending on the order in which they appear in the pipeline,
186+
aggregation operators can take advantage of indexes.
187+
188+
The following aggregation operators:
189+
190+
- :agg:pipeline:`$match`
191+
- :agg:pipeline:`$sort`
192+
- :agg:pipeline:`$limit`
193+
- :agg:pipeline:`$skip`
194+
195+
can take advantage of an index when placed at the **beginning** of the pipleline or
196+
placed **before** the following aggregation operators:
197+
198+
- :agg:pipeline:`$project`
199+
- :agg:pipeline:`$unwind`
200+
- :agg:pipeline:`$group`.
201+
202+
Early Filtering
203+
~~~~~~~~~~~~~~~
204+
185205
If your aggregation operation requires only a subset of the data in a
186206
collection, use the :agg:pipeline:`$match` operator to restrict which items go
187207
in to the top of the pipeline, as in a query. When placed early in a
188208
pipeline, these :agg:pipeline:`$match` operations use suitable indexes
189209
to scan only the matching documents in a collection.
190210

211+
Placing :agg:pipeline:`$match` then :agg:pipeline:`$sort` at the very
212+
start of the pipeline would be logically equivalent to a single query
213+
and a sort, and be able to use an index.
214+
191215
.. OMMITED: this feature is pending SERVER-4506. Other optimizations
192216
.. are pending SERVER-4507 SERVER-4644 SERVER-4656 SERVER-4816
193217
..

source/reference/aggregation/sort.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ $sort
4141

4242
.. TODO mention the importance of order preserving objects
4343

44+
- :agg:pipeline:`$skip`
45+
46+
:pipeline:`$sort` operator can take advantage of an index when
47+
placed at the **beginning** of the pipleline or placed **before**
48+
the following aggregation operators:
49+
50+
- :agg:pipeline:`$project`
51+
- :agg:pipeline:`$unwind`
52+
- :agg:pipeline:`$group`.
53+
4454
.. warning:: Unless the :pipeline:`$sort` operator can use an index,
4555
in the current release, the sort must fit within memory. This
4656
may cause problems when sorting large numbers of documents.

0 commit comments

Comments
 (0)