0
0
MongoDBquery~5 mins

Combining comparison operators in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does combining comparison operators in MongoDB allow you to do?
It allows you to filter documents by applying multiple conditions on the same field, such as finding values greater than one number and less than another.
Click to reveal answer
beginner
How do you combine comparison operators like $gt and $lt in a MongoDB query?
You put them inside the same field object, for example: { age: { $gt: 20, $lt: 30 } } to find ages greater than 20 and less than 30.
Click to reveal answer
intermediate
What is the difference between using $and operator and combining comparison operators inside one field in MongoDB?
$and combines multiple conditions that can be on different fields, while combining comparison operators inside one field applies multiple conditions on the same field.
Click to reveal answer
beginner
Example: How to find documents where price is between 10 and 50 in MongoDB?
Use: { price: { $gte: 10, $lte: 50 } } which means price greater than or equal to 10 and less than or equal to 50.
Click to reveal answer
intermediate
Can you combine $ne (not equal) with other comparison operators on the same field in MongoDB?
Yes, for example: { score: { $ne: 100, $gt: 50 } } finds scores not equal to 100 and greater than 50.
Click to reveal answer
How do you write a MongoDB query to find documents where age is greater than 18 and less than 30?
A{ age: { $gt: 18 }, age: { $lt: 30 } }
B{ age: { $gt: 18, $lt: 30 } }
C{ $and: [ { age: { $gt: 18 } }, { age: { $lt: 30 } } ] }
D{ age: { $gte: 18, $lte: 30 } }
Which operator is used to combine multiple conditions on different fields in MongoDB?
A$lt
B$or
C$gt
D$and
What does this query do? { price: { $gte: 100, $lte: 200 } }
AFinds documents with price exactly 100 or 200
BFinds documents with price less than 100 or greater than 200
CFinds documents with price between 100 and 200 inclusive
DFinds documents with price not equal to 100 and 200
Is this query valid? { score: { $ne: 50, $gt: 30 } }
AYes, it finds scores not equal to 50 and greater than 30
BNo, you cannot combine $ne with other operators
CNo, $ne must be used alone
DYes, but it finds scores equal to 50 or greater than 30
What is the difference between { age: { $gt: 20, $lt: 30 } } and { $and: [ { age: { $gt: 20 } }, { age: { $lt: 30 } } ] }?
AThey are equivalent queries
BFirst is invalid syntax
CSecond only works for different fields
DFirst only works for strings
Explain how to combine multiple comparison operators on the same field in a MongoDB query.
Think about putting conditions inside one object for the field.
You got /3 concepts.
    Describe the difference between using $and operator and combining comparison operators inside one field in MongoDB.
    Consider when you want to filter by one field versus multiple fields.
    You got /3 concepts.