Visit complete MongoDB roadmap

← Back to Topics List


The $match operator is used to filter documents within the pipeline in the MongoDB aggregation framework. It helps in excluding documents that do not fulfill the specified condition(s). The $match operator filters documents and passes only those that match the specified conditions to the next stage of the pipeline.

The basic syntax for the $match operator is as follows:

{ $match: { <query> } }

Where <query> contains the conditions and the fields which the documents should match.


Let’s take a look at some examples to understand the usage of the $match operator.

Suppose you have a collection named employees with the following document structure:

  "_id": ObjectId("123"),
  "firstName": "John",
  "lastName": "Doe",
  "age": 25,
  "department": "HR"

You are asked to find employees aged above 30. To do this, you can use the $match operator as follows:

  { $match: { age: { $gt: 30 } } }

This returns all employees with age greater than 30.

Example 2:

Now, let’s say you also want to filter employees working in the “HR” department. You can chain conditions to the $match operator like this:

  { $match: { age: { $gt: 30 }, department: "HR" } }

This returns employees who are aged above 30 and working in the “HR” department.

Important Things to Keep in Mind

  • When using multiple conditions in the $match query, they work as an implicit $and operator.
  • $match operator works best earlier in the pipeline. Placing it earlier prevents unnecessary processing and filtering of documents in later stages, which can improve the overall performance of the aggregation pipeline.
  • The $match operator uses most of the standard query operators, like $gt, $lte, $in, and so on.

In conclusion, the $match operator is a powerful and essential tool when working with MongoDB’s aggregation pipeline to filter and process datasets based on specific conditions, leading to better performance and more relevant results.

Community is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© · FAQs · Terms · Privacy


The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.