For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

$match (aggregation stage)

$match

Filters documents based on a specified query predicate. Matched documents are passed to the next pipeline stage.

You can use $match for deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
{ $match: { <query predicate> } }

The syntax for the $match query predicate is identical to the syntax used in the query argument of a find() command.

To include expressions in a query predicate, use the $expr operator.

A $match stage filters out a document from pipeline results if one of the following conditions applies:

  • The $match query predicate returns a 0, null, or false value on that document.

  • The $match query predicate uses a field that is missing from that document.

  • You cannot use $where in a $match stage.

  • You cannot use $near or $nearSphere in a $match stage. As an alternative, you can either:

  • To use $text in a $match stage, the $match stage has to be the first stage of the pipeline.

    Views do not support $text.

    Note

    $text provides text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, MongoDB Search.

For data stored in MongoDB Atlas, you can use the MongoDB Search compound Operator operator filter option to match or filter documents when running $search queries. Running $match after $search is less performant than running $search with the compound Operator operator filter option.

To learn more about the filter option, see compound Operator in the Atlas documentation.

The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

The following operation uses $match to perform an equality match on the rated field. The runtime filter limits the result to a small, representative set:

db.movies.aggregate(
[ { $match : { rated : "TV-PG", runtime : { $gt: 1000 } } } ]
)

The $match selects the documents where the rated field equals "TV-PG" and runtime is greater than 1000.

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:

db.movies.aggregate( [
{ $match: { $or: [
{ runtime: { $gt: 1000 } },
{ year: { $lt: 1910 } }
] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] )
[ { _id: null, count: 6 } ]

In the aggregation pipeline, $match selects the documents where either the runtime is greater than 1000 or the year is earlier than 1910. These documents are then sent to the $group to perform a count.

To filter documents based on elements in an array field, use the $elemMatch operator in the query predicate of the $match stage:

db.aggregate( [
{
$documents: [
{ student_id: 1, scores: [ 0.75, 0.65, 0.73 ] },
{ student_id: 2, scores: [ 0.9, 0.88, 0.98 ] },
{ student_id: 3, scores: [ 0.9, 0.84, 0.93 ] }
]
}, {
$match: {
scores: { $elemMatch: { $gte: 0.9 } }
}
}
] )
[
{ student_id: 2, scores: [ 0.9, 0.88, 0.98 ] },
{ student_id: 3, scores: [ 0.9, 0.84, 0.93 ] }
]

Refer to the Complete Aggregation Pipeline Tutorials for more information and use cases on aggregation.