MongoDB $match Operator

Last Updated : 29 Sep, 2025

In MongoDB, the $match operator is used to filter documents in the aggregation pipeline. It allows us to specify conditions that documents must meet before moving to the next stage of the pipeline. The $match operator is similar to the find() query, but it works within the aggregation framework, enabling more complex operations.

Syntax:

{ $match: { <query predicate> } }

In the above syntax:

  • The query predicate can be any valid MongoDB query.
  • The structure and operators inside the $match stage mirror those used in the find() method.

MongoDB $match Pipeline Stage

The $match operator works within an aggregation pipeline, which is a multi-stage data processing pipeline. Each stage performs a specific operation on the data, and $match is typically used as the first stage to limit the documents to a relevant subset.

Example of Basic $match Operation:

db.collection.aggregate([
{ $match: { status: "active" } }
]);

In this example:

  • Uses the $match stage in an aggregation pipeline.
  • Filters documents where status is "active".
  • Only matching documents are passed to the next pipeline stage.

Using $match for Equality Match

The $match operator is frequently used for equality matching, where we filter documents that exactly match a specified value in one or more fields. This is a basic yet powerful use case of $match, allowing us to easily find documents that meet a precise condition, such as an exact match on a field like "name" or "status."

Example: Filtering by Field Equality:

db.empdetails.aggregate([
{ $match: { salary: 9000 } }
]);

Output:

{
"_id": ObjectId("5541ffb465713ddc838b2dc7"),
"emp_code": "E005",
"emp_name": "Alan Hogg",
"date_of_join": "15/09/2013",
"salary": 9000,
"deduction": {
"pf": 2000,
"pt": 300,
"it": 200
}
}

Explanation: This query will return all documents from the empdetails collection where the salary field equals 9000.

Using $match with Logical Operators

MongoDB’s $match operator can be combined with logical operators like $or, $and, and $not to create more complex query conditions. These operators help to filter documents based on multiple criteria, allowing for more flexible and powerful querying in the aggregation pipeline.

Example: $match with $or Operator:

db.empdetails.aggregate([
{ $match: { $or: [{ salary: { $gt: 9000 } }, { emp_name: "Karlos Mint" }] } }
]);

Output:

{
"_id": ObjectId("5542003c65713ddc838b2dc8"),
"emp_code": "E006",
"emp_name": "Karlos Mint",
"date_of_join": "23/05/2010",
"salary": 12000,
"deduction": {
"pf": 3000,
"pt": 300,
"it": 400
}
}

Explanation:

This query retrieves documents where either the salary is greater than 9000 or the employee's name is "Karlos Mint".

Using $match with Comparison Operators

MongoDB's $match operator can be used with various comparison operators to filter documents based on numerical, string, or date values. Operators like $gt, $lt, $gte, $lte, and $ne allow us to compare fields against specific values and retrieve only those documents that meet the specified conditions, providing flexibility in querying data.

Example:

db.empdetails.aggregate([
{ $match: { salary: { $gt: 9000, $lt: 12000 } } }
]);

Output:

{
"_id": ObjectId("5542003c65713ddc838b2dc8"),
"emp_code": "E006",
"emp_name": "Karlos Mint",
"date_of_join": "23/05/2010",
"salary": 12000,
"deduction": {
"pf": 3000,
"pt": 300,
"it": 400
}
}

Explanation: This query filters documents where the salary is greater than 9000 and less than 12000.

Using $match with Date Fields

MongoDB provides support for date comparisons in $match. We can use operators to filter documents based on date ranges. By using comparison operators like $gt, $lt, $gte, and $lte, we can easily retrieve documents that fall within a certain date range, making it ideal for time-based filtering.

Example:

db.empdetails.aggregate([
{ $match: { date_of_join: { $gt: "01/01/2011" } } }
]);

Explanation: This query filters documents where the date_of_join is after January 1st, 2011.

Optimization in Aggregation Pipeline

To optimize aggregation performance in MongoDB, follow these best practices:

  • Place $match Early: Place the $match stage first to filter documents early, reducing workload and speeding up aggregation.
  • Use Indexes with $match: Placing $match first allows MongoDB to use indexes, so indexing frequently queried fields can speed up aggregation.


Comment
Article Tags:

Explore