A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mongodb/mongodb-and-operator-and/ below:

MongoDB AND operator ( $and )

MongoDB AND operator ( $and )

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.

What is the $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.

Syntax: 

{ $and: [ { Expression1 }, { Expression2 }, ..., { ExpressionN } ] }

or

{ Expression1, Expression2, ..., ExpressionN }

When to Use the $and Operator

Use the $and operator when:

  1. We need to filter documents based on multiple conditions.
  2. We are dealing with complex queries where each condition must be true for the documents to match.
  3. We need to combine conditions that involve the same field or different operators.
Examples of MongoDB AND Opeartor

Let’s go into some practical use cases and MongoDB queries that utilize the $and operator. In the following examples, we are working with:

Example 1: Matching values using $and operator:

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.

2. Using $and operator with multiple expressions specifying the same field :

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 ECEor the joiningYear is 2017, and the personal.state is UPor 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:

Advantages of Using the $and Operator Conclusion

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