Bird
Raised Fist0
MongoDBquery~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the MongoDB operator $lt do in a query?
easy
A. Finds documents where the field value is less than a specified value
B. Finds documents where the field value is greater than a specified value
C. Finds documents where the field value is equal to a specified value
D. Finds documents where the field value is not equal to a specified value

Solution

  1. Step 1: Understand the meaning of $lt

    The $lt operator means "less than" in MongoDB queries.
  2. Step 2: Apply the operator meaning to query behavior

    It selects documents where the field value is strictly less than the given value.
  3. Final Answer:

    Finds documents where the field value is less than a specified value -> Option A
  4. Quick Check:

    $lt means less than [OK]
Hint: Remember: lt = less than, lte = less than or equal [OK]
Common Mistakes:
  • Confusing $lt with $lte
  • Thinking $lt means less than or equal
  • Mixing $lt with greater than operators
2. Which of the following is the correct MongoDB query syntax to find documents where the field age is less than or equal to 30?
easy
A. { age: { $lte: 30 } }
B. { age: { $lt: 30 } }
C. { age: { $gt: 30 } }
D. { age: { $gte: 30 } }

Solution

  1. Step 1: Identify the operator for less than or equal

    The operator $lte means "less than or equal to" in MongoDB.
  2. Step 2: Match the operator with the query syntax

    The correct syntax to find documents where age is less than or equal to 30 is { age: { $lte: 30 } }.
  3. Final Answer:

    { age: { $lte: 30 } } -> Option A
  4. Quick Check:

    $lte means less than or equal [OK]
Hint: Use $lte for less than or equal, $lt for strictly less [OK]
Common Mistakes:
  • Using $lt instead of $lte for less than or equal
  • Using greater than operators by mistake
  • Incorrect query object structure
3. Given the collection products with documents:
{ "name": "Pen", "price": 5 }
{ "name": "Notebook", "price": 10 }
{ "name": "Bag", "price": 20 }

What will be the result of the query db.products.find({ price: { $lt: 10 } })?
medium
A. []
B. [{ "name": "Pen", "price": 5 }, { "name": "Notebook", "price": 10 }]
C. [{ "name": "Pen", "price": 5 }]
D. [{ "name": "Notebook", "price": 10 }, { "name": "Bag", "price": 20 }]

Solution

  1. Step 1: Understand the query condition

    The query uses $lt: 10, so it finds documents where price is less than 10.
  2. Step 2: Check each document's price

    Only the "Pen" has price 5, which is less than 10. "Notebook" has price 10, which is not less than 10.
  3. Final Answer:

    [{ "name": "Pen", "price": 5 }] -> Option C
  4. Quick Check:

    Only prices less than 10 included [OK]
Hint: Remember $lt excludes equal values, only strictly less [OK]
Common Mistakes:
  • Including documents with price equal to 10
  • Confusing $lt with $lte
  • Returning empty result by mistake
4. You wrote this query to find documents where score is less than or equal to 50:
db.scores.find({ score: { $lt: 50 } })
But you want to include documents where score is exactly 50. What is the error and how to fix it?
medium
A. Use $gt instead of $lt
B. No error, query is correct
C. Add $eq: 50 inside the query
D. Use $lte instead of $lt to include 50

Solution

  1. Step 1: Identify the operator used

    The query uses $lt, which excludes values equal to 50.
  2. Step 2: Choose the correct operator to include 50

    To include values equal to 50, use $lte (less than or equal).
  3. Final Answer:

    Use $lte instead of $lt to include 50 -> Option D
  4. Quick Check:

    $lte includes equal values [OK]
Hint: Use $lte to include the boundary value [OK]
Common Mistakes:
  • Using $lt and expecting equal values included
  • Using $gt which is wrong direction
  • Trying to combine $lt and $eq unnecessarily
5. You have a collection orders with documents containing total values. You want to find all orders with total less than or equal to 100 but greater than 50. Which query correctly uses $lt and $lte to achieve this?
hard
A. { total: { $gte: 50, $lt: 100 } }
B. { total: { $gt: 50, $lte: 100 } }
C. { total: { $lt: 100, $lte: 50 } }
D. { total: { $lt: 100, $gt: 50 } }

Solution

  1. Step 1: Understand the range needed

    We want totals greater than 50 and less than or equal to 100.
  2. Step 2: Match operators to conditions

    $gt: 50 means strictly greater than 50, $lte: 100 means less than or equal to 100.
  3. Step 3: Check query syntax

    { total: { $gt: 50, $lte: 100 } } correctly uses both operators in one object: { total: { $gt: 50, $lte: 100 } }.
  4. Final Answer:

    { total: { $gt: 50, $lte: 100 } } -> Option B
  5. Quick Check:

    Use $gt for greater than, $lte for less than or equal [OK]
Hint: Combine $gt and $lte for range queries [OK]
Common Mistakes:
  • Mixing $lt and $lte incorrectly
  • Using $gte instead of $gt when strict greater needed
  • Reversing operator directions