@@ -6,56 +6,90 @@ $match (aggregation)
6
6
7
7
.. pipeline:: $match
8
8
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.
14
11
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.
17
14
18
- .. code-block:: javascript
15
+ .. example::
19
16
20
- db.article.aggregate(
21
- { $match : <match-predicate> }
22
- );
17
+ The following operation uses :pipeline:`$match` to perform a
18
+ simple equality match:
23
19
24
- The following example performs a simple field equality test:
20
+ .. code-block:: javascript
25
21
26
- .. code-block:: javascript
22
+ db.articles.aggregate(
23
+ { $match : { author : "dave" } }
24
+ );
27
25
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:
31
28
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
35
30
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 }
37
43
38
- db.article.aggregate(
39
- { $match : { score : { $gt : 50, $lte : 90 } } }
40
- );
44
+ .. example::
41
45
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 }
44
74
45
75
.. note::
46
76
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.
55
91
56
92
.. warning::
57
93
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`
60
95
queries as part of the aggregation pipeline.
61
-
0 commit comments