@@ -174,20 +174,44 @@ Document size <limit-bson-document-size>` limit, which is currently 16 megabytes
174
174
Optimizing Performance
175
175
----------------------
176
176
177
- Early Filtering
178
- ~~~~~~~~~~~~~~~
179
-
180
177
Because you will always call :method:`aggregate` on a
181
178
:term:`collection` object, which logically inserts the *entire* collection into
182
179
the aggregation pipeline, you may want to optimize the operation
183
180
by avoiding scanning the entire collection whenever possible.
184
181
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
+
185
205
If your aggregation operation requires only a subset of the data in a
186
206
collection, use the :agg:pipeline:`$match` operator to restrict which items go
187
207
in to the top of the pipeline, as in a query. When placed early in a
188
208
pipeline, these :agg:pipeline:`$match` operations use suitable indexes
189
209
to scan only the matching documents in a collection.
190
210
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
+
191
215
.. OMMITED: this feature is pending SERVER-4506. Other optimizations
192
216
.. are pending SERVER-4507 SERVER-4644 SERVER-4656 SERVER-4816
193
217
..
0 commit comments