Concept Flow - $and operator behavior
Start Query
Evaluate Each Condition
Are All Conditions True?
No→Return No Match
Yes
Return Document Match
The $and operator checks multiple conditions and returns documents only if all conditions are true.
Jump into concepts and practice - no test required
{ $and: [ { age: { $gt: 20 } }, { city: 'NY' } ] }| Step | Document | Condition 1 (age > 20) | Condition 2 (city = 'NY') | All Conditions True? | Result |
|---|---|---|---|---|---|
| 1 | { age: 25, city: 'NY' } | True | True | True | Match |
| 2 | { age: 18, city: 'NY' } | False | True | False | No Match |
| 3 | { age: 30, city: 'LA' } | True | False | False | No Match |
| 4 | { age: 22, city: 'NY' } | True | True | True | Match |
| 5 | { age: 20, city: 'NY' } | False | True | False | No Match |
| Document | Condition 1 (age > 20) | Condition 2 (city = 'NY') | All Conditions True? |
|---|---|---|---|
| { age: 25, city: 'NY' } | True | True | True |
| { age: 18, city: 'NY' } | False | True | False |
| { age: 30, city: 'LA' } | True | False | False |
| { age: 22, city: 'NY' } | True | True | True |
| { age: 20, city: 'NY' } | False | True | False |
$and operator syntax:
{ $and: [ condition1, condition2, ... ] }
Returns documents only if ALL conditions are true.
If any condition is false, document is excluded.
Useful to combine multiple filters logically.What does the $and operator do in a MongoDB query?
$and$and operator combines multiple conditions and requires all to be true for a document to match.$or, which matches if any condition is true, $and needs all conditions true.$and means all conditions must match [OK]Which of the following is the correct syntax to use $and in a MongoDB query?
{ $and: [ { age: { $gt: 20 } }, { city: "NY" } ] }$and$and operator requires an array of condition objects inside square brackets.$and needs an array of conditions [OK]Given the collection users with documents:
[{ name: "Alice", age: 25, city: "NY" }, { name: "Bob", age: 30, city: "LA" }, { name: "Carol", age: 25, city: "LA" }]What will the query { $and: [ { age: 25 }, { city: "LA" } ] } return?
age is 25 AND city is "LA".Consider this query:
{ $and: { age: { $gt: 20 }, city: "NY" } }What is wrong with this query?
$and operator requires an array of conditions, but here it is given an object.$and needs an array of conditions [OK]You want to find documents in a products collection where the price is greater than 100 and the category is either "electronics" or "appliances". Which query correctly uses $and and $or to achieve this?
$and with price condition and an inner $or for categories. { $or: [ { price: { $gt: 100 } }, { category: "electronics" }, { category: "appliances" } ] } uses $or for all, which is incorrect. { price: { $gt: 100 }, category: { $or: [ "electronics", "appliances" ] } } uses invalid syntax for $or inside category. { $and: { price: { $gt: 100 }, category: { $in: [ "electronics", "appliances" ] } } } uses $and with an object instead of array, which is invalid.