Skip to content

Commit 43cb7c2

Browse files
committed
DOCS-968 within in match agg operator
1 parent d7ee682 commit 43cb7c2

File tree

3 files changed

+74
-37
lines changed

3 files changed

+74
-37
lines changed

source/reference/aggregation/match.txt

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,90 @@ $match (aggregation)
66

77
.. pipeline:: $match
88

9-
Provides a query-like interface to filter documents out of the
10-
aggregation :term:`pipeline`. The :pipeline:`$match` drops
11-
documents that do not match the condition from the aggregation
12-
pipeline, and it passes documents that match along the pipeline
13-
unaltered.
9+
:pipeline:`$match` pipes the documents that match its conditions to
10+
the next operator in the pipeline.
1411

15-
The syntax passed to the :pipeline:`$match` is identical
16-
to the :term:`query` syntax. Consider the following prototype form:
12+
The :pipeline:`$match` query syntax is identical to the
13+
:ref:`read operation query <read-operations-query-argument>` syntax.
1714

18-
.. code-block:: javascript
15+
.. example::
1916

20-
db.article.aggregate(
21-
{ $match : <match-predicate> }
22-
);
17+
The following operation uses :pipeline:`$match` to perform a
18+
simple equality match:
2319

24-
The following example performs a simple field equality test:
20+
.. code-block:: javascript
2521

26-
.. code-block:: javascript
22+
db.articles.aggregate(
23+
{ $match : { author : "dave" } }
24+
);
2725

28-
db.article.aggregate(
29-
{ $match : { author : "dave" } }
30-
);
26+
The :pipeline:`$match` selects the documents where the ``author``
27+
field equals ``dave``, and the aggregation returns the following:
3128

32-
This operation only returns documents where the ``author`` field
33-
holds the value ``dave``. Consider the following example,
34-
which performs a range test:
29+
.. code-block:: javascript
3530

36-
.. code-block:: javascript
31+
{ "result" : [
32+
{
33+
"_id" : ObjectId("512bc95fe835e68f199c8686"),
34+
"author": "dave",
35+
"score" : 80
36+
},
37+
{ "_id" : ObjectId("512bc962e835e68f199c8687"),
38+
"author" : "dave",
39+
"score" : 85
40+
}
41+
],
42+
"ok" : 1 }
3743

38-
db.article.aggregate(
39-
{ $match : { score : { $gt : 50, $lte : 90 } } }
40-
);
44+
.. example::
4145

42-
Here, all documents return when the ``score`` field holds a value
43-
that is greater than 50 and less than or equal to 90.
46+
The following example selects documents to process using the
47+
:pipeline:`$match` pipeline operator and then pipes the results
48+
to the :pipeline:`$group` pipeline operator to compute a count of
49+
the documents:
50+
51+
.. code-block:: javascript
52+
53+
db.articles.aggregate( [
54+
{ $match : { score : { $gt : 70, $lte : 90 } } },
55+
{ $group: { _id: null, count: { $sum: 1 } } }
56+
] );
57+
58+
In the aggregation pipeline, :pipeline:`$match` selects the
59+
documents where the ``score`` is greater than ``70`` and less
60+
than or equal to ``90``. These documents are then piped to the
61+
:pipeline:`$group` to perform a count. The aggregation returns
62+
the following:
63+
64+
.. code-block:: javascript
65+
66+
{
67+
"result" : [
68+
{
69+
"_id" : null,
70+
"count" : 3
71+
}
72+
],
73+
"ok" : 1 }
4474

4575
.. note::
4676

47-
Place the :pipeline:`$match` as early in the aggregation
48-
:term:`pipeline` as possible. Because :pipeline:`$match`
49-
limits the total number of documents in the aggregation
50-
pipeline, earlier :pipeline:`$match` operations minimize the
51-
amount of later processing. If you place a :pipeline:`$match`
52-
at the very beginning of a pipeline, the query can take
53-
advantage of :term:`indexes <index>` like any other
54-
:mongodb:method:`db.collection.find()` or :mongodb:method:`db.collection.findOne()`.
77+
- Place the :pipeline:`$match` as early in the aggregation
78+
:term:`pipeline` as possible. Because :pipeline:`$match` limits
79+
the total number of documents in the aggregation pipeline,
80+
earlier :pipeline:`$match` operations minimize the amount of
81+
processing down the pipe.
82+
83+
- If you place a :pipeline:`$match` at the very beginning of a
84+
pipeline, the query can take advantage of :term:`indexes
85+
<index>` like any other :mongodb:method:`db.collection.find()`
86+
or :mongodb:method:`db.collection.findOne()`.
87+
88+
.. versionchanged:: 2.4
89+
Prior to 2.4, :pipeline:`$match` queries did **not** support the
90+
geospatial :mongodb:operator:`$within` operator.
5591

5692
.. warning::
5793

58-
You cannot use :mongodb:operator:`$where` or :ref:`geospatial
59-
operations <geospatial-query-operators>` in :pipeline:`$match`
94+
You cannot use :mongodb:operator:`$where` in :pipeline:`$match`
6095
queries as part of the aggregation pipeline.
61-

source/reference/operator/within.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ $within
5757
of its fields match the query.
5858

5959
.. include:: /includes/note-geospatial-index-must-exist.rst
60+
61+
.. versionchanged:: 2.4
62+
Prior to 2.4, the aggregation pipeline :agg:pipeline:`$match` queries did not support the
63+
:mongodb:operator:`$within` operator.

source/release-notes/2.4.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,4 +1002,3 @@ Improvements to the Aggregation Framework
10021002

10031003
- New :agg:expression:`$millisecond` operator to return the millisecond
10041004
portion of a date.
1005-

0 commit comments

Comments
 (0)