- Reference >
- Query, Update, Projection, and Aggregation Operators >
- $match (aggregation)
$match (aggregation)¶
-
$match
¶ Provides a query-like interface to filter documents out of the aggregation pipeline. The
$match
drops documents that do not match the condition from the aggregation pipeline, and it passes documents that match along the pipeline unaltered.The syntax passed to the
$match
is identical to the query syntax. Consider the following prototype form:The following example performs a simple field equality test:
This operation only returns documents where the
author
field holds the valuedave
. Consider the following example, which performs a range test:Here, all documents return when the
score
field holds a value that is greater than 50 and less than or equal to 90.Note
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 later processing. 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()
.Warning
You cannot use
$where
or geospatial operations in$match
queries as part of the aggregation pipeline.