0
0
MongoDBquery~10 mins

$lt and $lte for less than in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $lt and $lte for less than
Start Query
Check each document
Compare field value
$lt: value < target?
YesInclude document
Exclude document
$lte: value <= target?
YesInclude document
No
Exclude document
The query checks each document's field value against a target using $lt (less than) or $lte (less than or equal). Documents passing the test are included in the result.
Execution Sample
MongoDB
db.products.find({ price: { $lt: 50 } })
db.products.find({ price: { $lte: 50 } })
Finds products with price less than 50, and less than or equal to 50 respectively.
Execution Table
StepDocumentpriceConditionResultIncluded in Output
1{_id:1, price:30}3030 < 50 ($lt)TrueYes
2{_id:2, price:50}5050 < 50 ($lt)FalseNo
3{_id:3, price:50}5050 <= 50 ($lte)TrueYes
4{_id:4, price:60}6060 < 50 ($lt)FalseNo
5{_id:4, price:60}6060 <= 50 ($lte)FalseNo
💡 All documents checked; inclusion depends on $lt or $lte condition result.
Variable Tracker
VariableStartAfter Doc 1After Doc 2After Doc 3After Doc 4Final
priceN/A3050506060
$lt condition resultN/ATrueFalseN/AFalseN/A
$lte condition resultN/AN/AN/ATrueFalseN/A
Key Moments - 2 Insights
Why does a document with price 50 fail $lt: 50 but pass $lte: 50?
Because $lt means strictly less than 50, so 50 is not less than 50 (false). $lte means less than or equal to 50, so 50 equals 50 (true). See execution_table rows 2 and 3.
Are documents with price greater than 50 ever included?
No, both $lt and $lte exclude values greater than 50. See execution_table rows 4 and 5 where price 60 is excluded.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at step 2, what is the result of the $lt condition for price 50?
ATrue
BUndefined
CFalse
DError
💡 Hint
Check the 'Condition' and 'Result' columns at step 2 in execution_table.
At which step does the $lte condition include a document with price 50?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for $lte condition results in execution_table rows.
If we change the target value from 50 to 60 in $lt, which document would now be included that was previously excluded?
ADocument with price 60
BDocument with price 50
CDocument with price 30
DNo change
💡 Hint
Consider how $lt compares price values to the new target 60.
Concept Snapshot
$lt and $lte operators in MongoDB queries:
- $lt: matches documents where field value is strictly less than target.
- $lte: matches documents where field value is less than or equal to target.
- Used in find queries to filter documents by numeric or date fields.
- Example: { price: { $lt: 50 } } finds documents with price < 50.
- Example: { price: { $lte: 50 } } finds documents with price ≤ 50.
Full Transcript
This visual execution shows how MongoDB's $lt and $lte operators work in queries. The query checks each document's field value against a target number. For $lt, only values strictly less than the target pass. For $lte, values less than or equal to the target pass. The execution table traces documents with prices 30, 50, and 60. Documents with price 30 pass both $lt and $lte conditions. Price 50 fails $lt but passes $lte. Price 60 fails both. The variable tracker shows how price and condition results change per document. Key moments clarify why 50 fails $lt but passes $lte, and why values above target are excluded. The quiz tests understanding of condition results and effects of changing target values. The snapshot summarizes usage and behavior of $lt and $lte in MongoDB queries.