TExcelTAD
Home › MongoDB › Aggregation $match

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.

js
{ $match: { <query> } }

Filtering input documents

mongosh · try it yourself
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():

mongosh · try it yourself
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":

mongosh · try it yourself
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:

mongosh · try it yourself
// 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: $expr can'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

  • $match can't refer to fields of other documents; use $lookup for that.
  • $where (JavaScript) is not allowed in $match, and $text is only allowed when $match is the first stage.

Test yourself

Which SQL clause corresponds to a $match placed after $group?

  1. WHERE
  2. HAVING
  3. ORDER BY
  4. JOIN

Why should a filtering $match usually come first?

  1. MongoDB requires it
  2. It reduces the documents later stages process and can use an index
  3. Later stages can't see filtered fields
  4. It sorts the input