0
0
MongoDBquery~5 mins

Combining logical and comparison operators in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $and operator do in MongoDB queries?
The $and operator combines multiple conditions and returns documents that satisfy all of them.
Click to reveal answer
beginner
How do you use comparison operators like $gt and $lt in MongoDB?
Comparison operators like $gt (greater than) and $lt (less than) are used inside query objects to filter documents based on field values.
Click to reveal answer
intermediate
Explain how to combine $or with comparison operators in a MongoDB query.
You use $or to specify multiple alternative conditions. Each condition can use comparison operators. The query returns documents matching any one of the conditions.
Click to reveal answer
beginner
What is the difference between $and and $or in MongoDB queries?
$and requires all conditions to be true for a document to match, while $or requires at least one condition to be true.
Click to reveal answer
beginner
How would you write a MongoDB query to find documents where age is greater than 25 and status is 'active'?
Use { $and: [ { age: { $gt: 25 } }, { status: 'active' } ] }. This finds documents where both conditions are true.
Click to reveal answer
Which operator in MongoDB returns documents that satisfy all given conditions?
A$nor
B$or
C$not
D$and
How do you check if a field 'score' is less than 50 in MongoDB?
A{ score: { $eq: 50 } }
B{ score: { $lt: 50 } }
C{ score: 50 }
D{ score: { $gt: 50 } }
What does the following query do? { $or: [ { age: { $gt: 30 } }, { status: 'active' } ] }
AFinds documents where age is greater than 30 and status is 'active'
BFinds documents where age is less than 30 or status is not 'active'
CFinds documents where age is greater than 30 or status is 'active'
DFinds documents where age equals 30 or status equals 'active'
Which operator would you use to combine conditions that all must be true?
A$and
B$or
C$nor
D$exists
How do you write a query to find documents where 'price' is between 10 and 20 inclusive?
A{ price: { $gte: 10, $lte: 20 } }
B{ price: { $lt: 10, $gt: 20 } }
C{ price: { $gt: 10, $lt: 20 } }
D{ price: { $eq: 10, $eq: 20 } }
Describe how to combine logical operators with comparison operators in a MongoDB query.
Think about how you can ask for multiple conditions at once.
You got /4 concepts.
    Explain the difference between $and and $or operators in MongoDB with examples.
    Think about 'all' versus 'any' conditions.
    You got /4 concepts.