Navigation
This version of the documentation is archived and no longer supported.

$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:

db.article.aggregate(
    { $match : <match-predicate> }
);

The following example performs a simple field equality test:

db.article.aggregate(
    { $match : { author : "dave" } }
);

This operation only returns documents where the author field holds the value dave. Consider the following example, which performs a range test:

db.article.aggregate(
    { $match : { score  : { $gt : 50, $lte : 90 } } }
);

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 other db.collection.find() or db.collection.findOne().

Warning

You cannot use $where or geospatial operations in $match queries as part of the aggregation pipeline.