0
0
MongoDBquery~10 mins

$gt and $gte for greater than in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $gt and $gte for greater than
Start Query
Check $gt or $gte condition
$gt: value > target?
Include document
Exclude document
$gte: value >= target?
Include document
Exclude document
Return filtered documents
The query checks each document's field against $gt or $gte conditions to include only those with values greater than (or equal to) the target.
Execution Sample
MongoDB
db.products.find({ price: { $gt: 50 } })
Finds all products with price greater than 50.
Execution Table
StepDocumentpriceConditionCondition ResultInclude in Result
1{_id:1, price:30}3030 > 50 ($gt)FalseNo
2{_id:2, price:50}5050 > 50 ($gt)FalseNo
3{_id:3, price:70}7070 > 50 ($gt)TrueYes
4{_id:4, price:50}5050 >= 50 ($gte)TrueYes
5{_id:5, price:49}4949 >= 50 ($gte)FalseNo
6End of documents----
💡 All documents checked; only those meeting condition included.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
price-3050705049-
Condition ($gt 50)-FalseFalseTrue---
Condition ($gte 50)----TrueFalse-
Included in Result-NoNoYesYesNo-
Key Moments - 2 Insights
Why does a document with price 50 fail $gt: 50 but pass $gte: 50?
Because $gt means strictly greater than, so 50 > 50 is false (see rows 2 and 3). $gte means greater than or equal, so 50 >= 50 is true (see row 4).
Does $gt include the target value itself?
No, $gt excludes the target value. Only values strictly greater than the target pass (see row 2 where 50 > 50 is false).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which document is included when using $gt: 50?
A{_id:3, price:70}
B{_id:2, price:50}
C{_id:1, price:30}
D{_id:5, price:49}
💡 Hint
Check the 'Condition Result' and 'Include in Result' columns for $gt condition in rows 1-3.
At which step does the $gte condition evaluate to true?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look at the 'Condition ($gte 50)' row in variable_tracker and match with execution_table step.
If we change $gt: 50 to $gte: 50, which document inclusion changes?
ADocument with price 30
BDocument with price 70
CDocument with price 50
DDocument with price 49
💡 Hint
Compare inclusion results for $gt and $gte in execution_table rows 2 and 4.
Concept Snapshot
$gt and $gte operators filter documents by comparing field values.
$gt means 'greater than' (strictly greater).
$gte means 'greater than or equal to'.
Use in queries like { field: { $gt: value } }.
Only documents meeting condition are returned.
$gte includes the target value; $gt does not.
Full Transcript
This visual execution shows how MongoDB's $gt and $gte operators work to filter documents. Each document's field value is compared to the target value. For $gt, only values strictly greater than the target pass. For $gte, values equal to or greater than the target pass. The execution table traces each document's price against these conditions, showing which documents are included or excluded. Key moments clarify why 50 fails $gt but passes $gte. The quiz tests understanding by referencing specific steps and variable states.