$lt and $lte for less than in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using $lt and $lte in MongoDB queries, it's important to know how the query time changes as the data grows.
We want to understand how the number of documents affects the time it takes to find those less than a value.
Analyze the time complexity of the following MongoDB query.
db.products.find({ price: { $lt: 100 } })
.toArray()
.then(results => console.log(results))
.catch(err => console.error(err));
This query finds all products with a price less than 100.
Look at what repeats when the query runs.
- Primary operation: Checking each document's price to see if it is less than 100.
- How many times: Once for each document in the collection if no index is used.
As the number of documents grows, the query checks more prices.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of documents.
Time Complexity: O(n)
This means the query time grows in direct proportion to the number of documents in the collection.
[X] Wrong: "Using $lt or $lte always makes the query fast no matter the data size."
[OK] Correct: Without an index, MongoDB must check every document, so the query slows down as data grows.
Understanding how simple comparison queries scale helps you explain database performance clearly and confidently.
What if we added an index on the price field? How would the time complexity change?
Practice
$lt do in a query?Solution
Step 1: Understand the meaning of
The$lt$ltoperator means "less than" in MongoDB queries.Step 2: Apply the operator meaning to query behavior
It selects documents where the field value is strictly less than the given value.Final Answer:
Finds documents where the field value is less than a specified value -> Option AQuick Check:
$ltmeans less than [OK]
- Confusing $lt with $lte
- Thinking $lt means less than or equal
- Mixing $lt with greater than operators
age is less than or equal to 30?Solution
Step 1: Identify the operator for less than or equal
The operator$ltemeans "less than or equal to" in MongoDB.Step 2: Match the operator with the query syntax
The correct syntax to find documents whereageis less than or equal to 30 is{ age: { $lte: 30 } }.Final Answer:
{ age: { $lte: 30 } } -> Option AQuick Check:
$ltemeans less than or equal [OK]
- Using $lt instead of $lte for less than or equal
- Using greater than operators by mistake
- Incorrect query object structure
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 } })?Solution
Step 1: Understand the query condition
The query uses$lt: 10, so it finds documents wherepriceis less than 10.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.Final Answer:
[{ "name": "Pen", "price": 5 }] -> Option CQuick Check:
Only prices less than 10 included [OK]
- Including documents with price equal to 10
- Confusing $lt with $lte
- Returning empty result by mistake
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?Solution
Step 1: Identify the operator used
The query uses$lt, which excludes values equal to 50.Step 2: Choose the correct operator to include 50
To include values equal to 50, use$lte(less than or equal).Final Answer:
Use$lteinstead of$ltto include 50 -> Option DQuick Check:
$lteincludes equal values [OK]
- Using $lt and expecting equal values included
- Using $gt which is wrong direction
- Trying to combine $lt and $eq unnecessarily
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?Solution
Step 1: Understand the range needed
We want totals greater than 50 and less than or equal to 100.Step 2: Match operators to conditions
$gt: 50means strictly greater than 50,$lte: 100means less than or equal to 100.Step 3: Check query syntax
{ total: { $gt: 50, $lte: 100 } } correctly uses both operators in one object:{ total: { $gt: 50, $lte: 100 } }.Final Answer:
{ total: { $gt: 50, $lte: 100 } } -> Option BQuick Check:
Use $gt for greater than, $lte for less than or equal [OK]
- Mixing $lt and $lte incorrectly
- Using $gte instead of $gt when strict greater needed
- Reversing operator directions
