0
0
MongoDBquery~10 mins

Comparison expressions ($eq, $gt in aggregation) in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Comparison expressions ($eq, $gt in aggregation)
Start aggregation pipeline
Apply $match stage
Evaluate $eq or $gt expression
Filter documents based on comparison
Output filtered documents
End
The aggregation pipeline starts, applies a $match stage where $eq or $gt expressions compare fields to values, filters documents accordingly, and outputs the result.
Execution Sample
MongoDB
db.products.aggregate([
  { $match: { price: { $gt: 100 } } }
])
Filters products with price greater than 100 using $gt in aggregation.
Execution Table
StepDocumentCondition EvaluatedResultAction
1{_id:1, price:50}price > 100FalseExclude document
2{_id:2, price:150}price > 100TrueInclude document
3{_id:3, price:100}price > 100FalseExclude document
4{_id:4, price:200}price > 100TrueInclude document
End---No more documents to evaluate
💡 All documents processed; only those with price > 100 included.
Variable Tracker
VariableStartAfter Doc 1After Doc 2After Doc 3After Doc 4Final
Included Documents[][][{_id:2, price:150}][{_id:2, price:150}][{_id:2, price:150}, {_id:4, price:200}][{_id:2, price:150}, {_id:4, price:200}]
Key Moments - 2 Insights
Why is the document with price 100 excluded when using $gt: 100?
Because $gt means 'greater than', not 'greater than or equal to'. The execution_table row 3 shows price 100 evaluated as False.
How does $eq differ from $gt in filtering documents?
$eq checks for exact equality, while $gt checks if a value is strictly greater. The execution_table shows $gt filtering; $eq would include only documents matching exactly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which document is included at step 2?
A{_id:2, price:150}
B{_id:1, price:50}
C{_id:3, price:100}
D{_id:4, price:200}
💡 Hint
Check the 'Result' and 'Action' columns at step 2 in execution_table.
At which step does the condition 'price > 100' become false for the first time?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Result' column in execution_table for step 1.
If we change $gt: 100 to $eq: 100, which document would be included?
A{_id:2, price:150}
B{_id:1, price:50}
C{_id:3, price:100}
D{_id:4, price:200}
💡 Hint
Refer to the document with price exactly 100 in execution_table.
Concept Snapshot
Aggregation $match uses comparison expressions like $eq and $gt.
$eq checks if a field equals a value.
$gt checks if a field is greater than a value.
Documents passing the condition are included in output.
Useful for filtering data in aggregation pipelines.
Full Transcript
This visual execution shows how MongoDB aggregation uses comparison expressions $eq and $gt inside a $match stage. Each document is checked against the condition, for example price > 100. Documents with price 150 and 200 pass and are included, while those with 50 and 100 do not. The variable tracker shows how the included documents list grows. Key moments clarify why 100 is excluded with $gt and how $eq differs. The quiz tests understanding of which documents pass and how changing the operator affects results.