MongoDB - Logical Query Operators
Last Updated : 03 Oct, 2025
In MongoDB, logical query operators combine multiple conditions within a single query. They enable complex filtering and allow more flexible and precise data retrieval. It is commonly used for advanced searches, conditional filtering, and data analysis scenarios.
Here is a detailed explanation of the most commonly used logical query operators in MongoDB. It Joins query clauses with a logical OR. Returns documents that match any of the conditions.
Operator | Description | Syntax |
---|
$and | It Joins query clauses with a logical AND. Returns documents that match all the conditions. | { $and: [ { <expression1> }, { <expression2> }, ... ] } |
$or | { $or: [ { <expression1> }, { <expression2> }, ... ] } |
$nor | It Joins query clauses with a logical NOR. Returns documents that fail to match all the conditions. | { $nor: [ { <expression1> }, { <expression2> }, ... ] } |
$not | It Inverts the effect of a query expression. Returns documents that do not match the query expression. | { <field>: { $not: { <operator-expression> } } } |
Examples of Logical Query Operators
Let’s go deeper into how each operator works with examples from a sample MongoDB collection. In the following examples, we are working with:
- Database: GeeksforGeeks
- Collection: contributor
- Document: three documents that contain the details of the contributors in the form of field-value pairs.

Example 1: Matching values using $and
operator
The $and
operator is used to combine multiple conditions, where all conditions must be met. 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 will only return documents where both conditions are true: the branch
is "CSE" and the joiningYear
is 2018.
Example 2: Matching values using $nor
operator
The $nor
operator is the opposite of $or
. It retrieves documents where none of the specified conditions are met. In this example, we are retrieving only those employee’s documents whose salary is not 3000 and whose branch is not ECE.
Query:
db.contributor.find({$nor: [{salary: 3000}, {branch: "ECE"}]}).pretty()
Output

Explanation: This query will return documents where neither of the conditions (salary: 3000
or branch: "ECE"
) is true.
Example 3: Matching values using $or
operator
The $or
operator combines multiple conditions, where at least one of the conditions must be true. In this example, we are retrieving only those employee’s documents whose branch is ECE or joiningYear is 2017.
Query:
db.contributor.find({$or: [{branch: "ECE"}, {joiningYear: 2017}]}).pretty()
Output

Explanation:
- This query returns documents where either the
branch
is "ECE" or the joiningYear
is 2017. - If either of the conditions is true, the document will be returned.
Example 4: Matching values using $not
operator
The $not
operator is used to negate the effect of a query expression. In this example, we are retrieving only those employee’s documents whose salary is not greater than 2000.
Query:
db.contributor.find({salary: {$not: {$gt: 2000}}}).pretty()
Output
Explanation:
- This query returns documents where the
salary
is less than or equal to 2000. - The
$gt
operator checks if the value is greater than 2000, and $not
negates this condition.
Explore
MongoDB Tutorial
7 min read
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators
My Profile