MongoDB Logical Query Operators (original) (raw)

Last Updated : 5 May, 2026

MongoDB logical query operators combine multiple conditions in a single query to enable flexible, precise filtering for advanced searches and data analysis.

MongoDB Logical Operators

Let’s understand how each operator works with examples from a sample MongoDB collection. In the following examples, we are working with:

Screenshot-2026-02-09-124834

1. $and operator

It Joins query clauses with a logical AND. Returns documents that match all the conditions.

**Syntax

{ $and: [ { }, { }, ... ] }

**Example: Retrieving only those employees' documents whose branch is CSE and joiningYear is 2018.

db.contributor.find({$and: [{branch: "CSE"}, {joiningYear: 2018}]})

**Output:

Screenshot-2026-02-09-124946

2. $nor operator

It Joins query clauses with a logical NOR. Returns documents that fail to match all the specified conditions (i.e., none of the conditions are true).

**Syntax

{ $nor: [ { }, { }, ... ] }

**Example: Retrieving only those employees' documents whose salary and branch is not 3000 and ECE respectively.

db.contributor.find({$nor: [{salary: 3000}, {branch: "ECE"}]})

**Output:

Screenshot-2026-02-09-125211

3. $or operator

The $or operator combines multiple conditions, where at least one of the conditions must be true.

**Syntax

{ $or: [ { }, { }, ... ] }

**Example: Retrieving only those employees' documents whose branch is ECE or joiningYear is 2017.

db.contributor.find({$or: [{branch: "ECE"}, {joiningYear: 2017}]})

**Output:

Screenshot-2026-02-09-125424

4. $not operator

It negates a condition at the field level and must be used with another operator (e.g., gt,gt, gt,eq). Returns documents that do not match the query expression.

**Syntax

{ : { $not: { } } }

**Example: Retrieving only those employee’s documents whose salary is not greater than 2000.

**Query:

db.contributor.find({salary: {$not: {$gt: 2000}}}).pretty()

**Output:

Screenshot-2026-02-09-140609