$match
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.
Examples
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:
db.employees.aggregate([
{ $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:
db.employees.aggregate([
{ $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
$matchquery, they work as an implicit$andoperator. $matchoperator 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
$matchoperator 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.