Last Updated : 12 Jul, 2025
MongoDB, a popular NoSQL database, offers several powerful query operators, including the $and
operator. This operator enables us to combine multiple conditions in a query to retrieve documents that satisfy all of the specified conditions. The $and
operator is a critical tool for building complex, precise, and efficient queries, making it an essential component of MongoDB querying.
$and
Operator in MongoDB?
The $and
operator is a logical operator in MongoDB used to combine multiple query conditions. It returns documents that match all of the specified conditions. The $and
operator helps refine queries, ensuring that only documents that meet all criteria are returned.
$and
. If the first condition evaluates to false
, MongoDB will not evaluate the remaining conditions. This behavior helps improve performance, especially in large collections.$and
: While MongoDB allows implicit use of the AND operation by simply listing conditions (separated by commas), the $and
operator is explicitly used when combining multiple conditions on the same field or operator.Syntax:
{ $and: [ { Expression1 }, { Expression2 }, ..., { ExpressionN } ] }
or
When to Use the{ Expression1, Expression2, ..., ExpressionN }
$and
Operator
Use the $and
operator when:
Let’s go into some practical use cases and MongoDB queries that utilize the $and
operator. In the following examples, we are working with:
In this example, we are retrieving only those employee’s documents whose branch is CSE and joiningYear is 2018.
Query:
db.contributor.find({$and: [{branch: "CSE"},
{joiningYear: 2018}]}).pretty()
Output
Explanation: This query ensures that both conditions are satisfied only documents where branch
is CSE
and joiningYear
is 2018
will be returned.
In this example, we are retrieving only those employee’s documents whose branch is CSE.
db.contributor.find({$and: [{branch: {$eq: "CSE"}},
{branch: {$exists: true}}]}).pretty()
or
db.contributor.find({branch: {$eq: "CSE", $exists: true}}).pretty()3. Using $and operator with multiple expressions specifying the same operator
MongoDB allows combining the $and
operator with other logical operators like $or
. Here’s an example where we want to find documents where either the branch
is ECE
or the joiningYear
is 2017
, and the personal.state
is UP
or the personal.age
is 25
.
Query:
db.contributor.find({$and: [{$or: [{branch: "ECE"}, {joiningYear: 2017}]},
{$or: [{"personal.state": "UP"},
{"personal.age": 25}]}]}).pretty()
Output
Explanation:
$or
condition checks if either branch
is ECE
or joiningYear
is 2017
.$or
condition checks if either personal.state
is UP
or personal.age
is 25
.$and
operator ensures that both $or
conditions must be satisfied for a document to match.$and
Operator
$and
operator allows you to specify multiple criteria that a document must satisfy, enabling more precise queries.$and
helps optimize query performance.$and
with other operators like $or
, $gt
, $lt
, $in
, etc., to create complex queries that fit your exact needs.The $and
operator is a fundamental tool for querying MongoDB databases with multiple conditions. It allows you to refine your queries, ensuring that only documents that meet all specified conditions are returned. With its support for both implicit and explicit usage, and its ability to combine with other operators like $or
, $gt
, and $lt
, the $and
operator is invaluable for performing complex searches. By addressing common use cases, providing detailed syntax, and offering solutions to potential queries, this content is tailored to rank higher in search results and provide value to MongoDB developers.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4