Learn how to use $and in $match MongoDB with clear code examples to filter data accurately and combine multiple query conditions effectively. Our Live Support Team is always here to help you.

How to Use $and in $match MongoDB for Precise Query Filtering

When working with MongoDB’s aggregation framework, developers often need to apply multiple conditions in a single query. This is where using $and in $match MongoDB becomes truly useful. It helps refine search results by combining several filters that must all be true for a document to pass through the pipeline. Let’s walk through how this works, step by step, with real code examples and a short explanation for each part.

using $and in $match mongodb

Understanding $match in MongoDB

In MongoDB’s aggregation pipeline, the $match stage is responsible for filtering documents before they move to the next stage. It works just like a query, allowing you to specify exact conditions. Its basic form looks like this:

{ $match: { <query> } }

The $match stage uses standard query syntax, the same one you use for read operations. However, keep in mind that $match does not accept raw aggregation expressions directly. If you want to use aggregation expressions, you’ll need to include them inside a $expr operator.

What $and Does


The $and operator evaluates multiple expressions and only returns documents that meet all of them. It’s like saying, “show me only the data that satisfies every single condition.”

Here’s how the syntax looks:

{ $and: [ <expression1>, <expression2>, ... ] }

Each expression inside the array acts as a condition that must return true.

Using $and in $match MongoDB

Now, let’s put everything together. You can use $and inside $match to combine several filters into one stage. Here’s the general structure:

db.collection.aggregate([
{
$match: {
$and: [
{ field1: value1 },
{ field2: value2 },
// Add more conditions as needed
]
}
}
]);

In this aggregation:

  • $match filters documents based on the given criteria.
  • $and ensures all listed conditions must be satisfied.
  • Each condition (like { field1: value1 }) defines what the query is checking for.

This approach is clean, direct, and perfect when you need multiple filters to work together.

Master MongoDB Queries Like Pro!

Chat animation


Real-World Example

To make it practical, let’s look at a real case. Suppose you have a collection called employees, and you need to find employees who are male and have a salary greater than 50,000. Using $and inside $match, your query would look like this:

db.employees.aggregate([
{
$match: {
$and: [
{ gender: "male" },
{ salary: { $gt: 50000 } }
]
}
}
]);

In this query:

  • The first condition filters all employees whose gender is “male.”
  • The second condition picks only those with salaries greater than 50,000.
  • The $and operator ensures both conditions must be met simultaneously.

This simple yet powerful combination helps you get precise data with minimal effort.

Why Use $and Explicitly

You might be wondering, “Do I really need to use $and?” In many cases, MongoDB treats multiple conditions in a single $match as an implicit AND operation. For instance:

{ $match: { gender: "male", salary: { $gt: 50000 } } }

This works the same way. However, using $and in $match MongoDB becomes necessary when you’re building more complex queries—especially those that involve mixing $or or nested logical operators. It gives you full control over how conditions are combined and executed.

Conclusion

The ability to combine multiple filters is one of the reasons MongoDB stands out for flexible data querying. By using $and in $match MongoDB, you can fine-tune your results, make complex conditions readable, and maintain cleaner aggregation pipelines.