0% completed
Logical operators in MongoDB are used to combine multiple query conditions. They allow you to create more complex queries by applying logical operations like AND, OR, NOT, and NOR to filter documents based on multiple criteria. Understanding how to use logical operators effectively is crucial for constructing flexible and powerful queries.
| Operator | Description |
|---|---|
$and | Joins query clauses with a logical AND and returns all documents that match the conditions of both clauses. |
$or | Joins query clauses with a logical OR and returns all documents that match the conditions of at least one clause. |
$not | Inverts the effect of a query expression and returns documents that do not match the query expression. |
$nor | Joins query clauses with a logical NOR and returns all documents that fail to match both clauses. |
The general syntax for using logical operators in MongoDB is:
db.collection.find( { <logicalOperator>: [ { <condition1> }, { <condition2> }, ... ] } )
First, let's insert some documents into the users collection to work with:
db.users.insertMany([ { name: "Alice", age: 25, email: "alice@example.com", scores: [85, 90, 88] }, { name: "Bob", age: 30, email: "bob@example.com", scores: [78, 82, 79] }, { name: "Charlie", age: 35, email: "charlie@example.com", scores: [92, 85, 90] }, { name: "David", age: 28, email: "david@example.com", scores: [88, 84, 89] }, { name: "Eve", age: 22, email: "eve@example.com", scores: [80, 86, 81] }, { name: "Frank", age: 30, email: "frank@example.com", scores: [75, 82, 78] }, { name: "Grace", age: 40, email: "grace@example.com", scores: [95, 91, 94] }, { name: "Heidi", age: 25, email: "heidi@example.com", scores: [85, 89, 87] } ])
This setup creates a users collection with various documents containing name, age, email, and scores fields. We will use these documents for demonstrating the logical operators in the subsequent sections.
The $and operator joins query clauses with a logical AND and returns all documents that match the conditions of both clauses.
Query for Age Greater Than 25 AND Less Than 35
db.users.find({ $and: [ { age: { $gt: 25 } }, { age: { $lt: 35 } } ] })
This command retrieves all documents where the age field is greater than 25 and less than 35.
Query for Name "Alice" AND Age 25
db.users.find({ $and: [ { name: "Alice" }, { age: 25 } ] })
This command retrieves all documents where the name is "Alice" and the age is 25.
The $or operator joins query clauses with a logical OR and returns all documents that match the conditions of at least one clause.
Query for Age Less Than 25 OR Greater Than 35
db.users.find({ $or: [ { age: { $lt: 25 } }, { age: { $gt: 35 } } ] })
This command retrieves all documents where the age field is less than 25 or greater than 35.
Query for Name "Alice" OR Age 30
db.users.find({ $or: [ { name: "Alice" }, { age: 30 } ] })
This command retrieves all documents where the name is "Alice" or the age is 30.
The $not operator inverts the effect of a query expression and returns documents that do not match the query expression.
Query for Age NOT Equal to 30
db.users.find({ age: { $not: { $eq: 30 } } })
This command retrieves all documents where the age field is not equal to 30.
Query for Name NOT "Alice"
db.users.find({ name: { $not: { $eq: "Alice" } } })
This command retrieves all documents where the name is not "Alice".
The $nor operator joins query clauses with a logical NOR and returns all documents that fail to match both clauses.
Query for Age NOT Less Than 25 NOR Greater Than 35
db.users.find({ $nor: [ { age: { $lt: 25 } }, { age: { $gt: 35 } } ] })
This command retrieves all documents where the age field is neither less than 25 nor greater than 35.
Query for Name NOT "Alice" NOR Age 30
db.users.find({ $nor: [ { name: "Alice" }, { age: 30 } ] })
This command retrieves all documents where the name is not "Alice" and the age is not 30.
.....
.....
.....