Introduction
Logical operators help you combine multiple conditions to find exactly what you want in your data.
Jump into concepts and practice - no test required
Logical operators help you combine multiple conditions to find exactly what you want in your data.
{ $and: [ { condition1 }, { condition2 } ] }
{ $or: [ { condition1 }, { condition2 } ] }
{ $not: { condition } }$and means all conditions must be true.
$or means at least one condition must be true.
$not means the condition must NOT be true.
{ $and: [ { age: { $gt: 18 } }, { city: 'New York' } ] }{ $or: [ { status: 'active' }, { score: { $gte: 90 } } ] }{ age: { $not: { $lt: 18 } } }This query finds students who have grade 'A' AND attendance of 90 or more.
db.students.find({ $and: [ { grade: 'A' }, { attendance: { $gte: 90 } } ] })Logical operators can be combined to build complex queries.
Using them correctly helps you get precise results and saves time.
Logical operators combine conditions to filter data.
$and requires all conditions to be true.
$or requires at least one condition to be true.
$not excludes documents matching a condition.
all conditions to be true for a document to match?age is greater than 25 or status is "active" in MongoDB?{ name: "Alice", age: 30, status: "active" }{ name: "Bob", age: 20, status: "inactive" }{ name: "Carol", age: 25, status: "active" }{ $and: [ { age: { $gt: 20 } }, { status: "active" } ] } return?status is "inactive":{ $not: { status: "inactive" } }age is greater than 20 but NOT with status equal to "inactive". Which query correctly uses logical operators to achieve this?