Aggregation $match
$match filters the documents in a pipeline. It takes the same query language as find(): equality, comparison operators, $or, $in, $elemMatch, dot notation and so on.
{ $match: { <query> } }Filtering input documents
print(db.products.aggregate([
{ $match: { category: 'equipment', 'specs.color': 'black' } },
{ $project: { name: 1, price: 1 } }
]).toArray());[
{ _id: 11, name: 'French Press', price: 35 },
{ _id: 13, name: 'Gooseneck Kettle', price: 59 }
]
Operators work exactly as in find():
print(db.orders.aggregate([
{ $match: {
status: { $in: ['shipped', 'processing'] },
date: { $gte: ISODate('2026-05-01T00:00:00Z') },
'items.productId': { $in: [1, 3] }
} },
{ $project: { customerId: 1, status: 1, total: 1 } }
]).toArray());[
{ _id: 1016, customerId: 3, status: 'processing', total: 73.5 },
{ _id: 1018, customerId: 11, status: 'processing', total: 83 }
]
$match at the start: the WHERE clause
A $match at the very beginning of the pipeline is the most efficient place: it reduces the number of documents every later stage must process, and it can use an index just like find(). MongoDB's optimiser even moves a $match forward by itself when that is safe.
$match after $group: the HAVING clause
A $match after $group filters the groups, using the fields the group created. This is how you say "customers with more than two orders":
print(db.orders.aggregate([
{ $group: { _id: '$customerId', orders: { $sum: 1 }, spent: { $sum: '$total' } } },
{ $match: { orders: { $gt: 2 } } },
{ $sort: { _id: 1 } }
]).toArray());[
{ _id: 1, orders: 3, spent: 142.5 },
{ _id: 3, orders: 3, spent: 178 },
{ _id: 5, orders: 3, spent: 209 }
]
Comparing fields with $expr
Plain $match compares fields with constant values. To compare two fields of the same document, or to use computed values, wrap an aggregation expression in $expr:
// orders whose average price per unit (total / units) is above 20, computed on the fly
print(db.orders.aggregate([
{ $match: { $expr: { $gt: [{ $divide: ['$total', { $sum: '$items.qty' }] }, 20] } } },
{ $project: { total: 1, units: { $sum: '$items.qty' } } }
]).toArray());[
{ _id: 1004, total: 89, units: 1 },
{ _id: 1006, total: 59, units: 1 },
{ _id: 1008, total: 59.5, units: 2 },
{ _id: 1015, total: 121, units: 3 },
{ _id: 1016, total: 73.5, units: 3 },
{ _id: 1018, total: 83, units: 3 }
]
Note:
$exprcan't use indexes as well as normal query conditions. Combine it with ordinary conditions when you can, so that those narrow the documents first.
Things $match can't do
$matchcan't refer to fields of other documents; use$lookupfor that.$where(JavaScript) is not allowed in$match, and$textis only allowed when$matchis the first stage.
Test yourself
Which SQL clause corresponds to a $match placed after $group?
- WHERE
- HAVING
- ORDER BY
- JOIN
Why should a filtering $match usually come first?
- MongoDB requires it
- It reduces the documents later stages process and can use an index
- Later stages can't see filtered fields
- It sorts the input