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.
{ $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.