- Reference >
- Operators >
- Aggregation Framework Operators >
- Pipeline Aggregation Operators >
- $match (aggregation)
$match (aggregation)¶
On this page
-
$match
¶ $match
pipes the documents that match its conditions to the next operator in the pipeline.The
$match
query syntax is identical to the read operation query syntax.
Examples¶
Equality Match¶
The following operation uses $match
to perform a
simple equality match:
The $match
selects the documents where the author
field equals dave
, and the aggregation returns the following:
Perform a Count¶
The following example selects documents to process using the
$match
pipeline operator and then pipes the results
to the $group
pipeline operator to compute a count of
the documents:
In the aggregation pipeline, $match
selects the
documents where the score
is greater than 70
and less
than or equal to 90
. These documents are then piped to the
$group
to perform a count. The aggregation returns
the following:
Behavior¶
Pipeline Optimization¶
- Place the
$match
as early in the aggregation pipeline as possible. Because$match
limits the total number of documents in the aggregation pipeline, earlier$match
operations minimize the amount of processing down the pipe. - If you place a
$match
at the very beginning of a pipeline, the query can take advantage of indexes like any otherdb.collection.find()
ordb.collection.findOne()
.