0
0
MongoDBquery~10 mins

$and operator behavior in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $and operator behavior
Start Query
Evaluate Each Condition
Are All Conditions True?
NoReturn No Match
Yes
Return Document Match
The $and operator checks multiple conditions and returns documents only if all conditions are true.
Execution Sample
MongoDB
{ $and: [ { age: { $gt: 20 } }, { city: 'NY' } ] }
Find documents where age is greater than 20 AND city is 'NY'.
Execution Table
StepDocumentCondition 1 (age > 20)Condition 2 (city = 'NY')All Conditions True?Result
1{ age: 25, city: 'NY' }TrueTrueTrueMatch
2{ age: 18, city: 'NY' }FalseTrueFalseNo Match
3{ age: 30, city: 'LA' }TrueFalseFalseNo Match
4{ age: 22, city: 'NY' }TrueTrueTrueMatch
5{ age: 20, city: 'NY' }FalseTrueFalseNo Match
💡 All documents checked; only those with all conditions true are matched.
Variable Tracker
DocumentCondition 1 (age > 20)Condition 2 (city = 'NY')All Conditions True?
{ age: 25, city: 'NY' }TrueTrueTrue
{ age: 18, city: 'NY' }FalseTrueFalse
{ age: 30, city: 'LA' }TrueFalseFalse
{ age: 22, city: 'NY' }TrueTrueTrue
{ age: 20, city: 'NY' }FalseTrueFalse
Key Moments - 3 Insights
Why does a document with age 18 and city 'NY' not match?
Because the first condition (age > 20) is false for that document, so $and returns false overall (see execution_table row 2).
If one condition is false, does $and still return true?
No, $and requires all conditions to be true. If any condition is false, the whole $and is false (see execution_table rows 2, 3, and 5).
Can $and be used with just one condition?
Yes, but it behaves like that single condition alone. $and is useful when combining multiple conditions (not shown in table but general behavior).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result for the document { age: 30, city: 'LA' }?
AMatch
BNo Match
CError
DPartial Match
💡 Hint
Check row 3 in the execution_table under 'Result' column.
At which step does the condition 'age > 20' become false?
AStep 2
BStep 4
CStep 1
DStep 3
💡 Hint
Look at the 'Condition 1 (age > 20)' column in execution_table.
If the second condition changed to city = 'LA', which document would match?
A{ age: 25, city: 'NY' }
B{ age: 18, city: 'NY' }
C{ age: 30, city: 'LA' }
D{ age: 22, city: 'NY' }
💡 Hint
Check which document has city 'LA' and age > 20 in variable_tracker.
Concept Snapshot
$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.
Full Transcript
The $and operator in MongoDB combines multiple conditions. It checks each condition on a document. If all conditions are true, the document matches. If any condition is false, the document does not match. For example, { $and: [ { age: { $gt: 20 } }, { city: 'NY' } ] } finds documents where age is greater than 20 and city is 'NY'. The execution table shows documents tested step-by-step, with condition results and final match decision. This helps beginners see how $and works internally.