$gt and $gte for greater than in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use $gt or $gte in MongoDB queries, we want to find documents with values greater than a number.
We ask: How does the time to find these documents grow as the collection gets bigger?
Analyze the time complexity of the following code snippet.
db.products.find({ price: { $gt: 100 } })
// or
db.products.find({ price: { $gte: 100 } })
This code finds all products with a price greater than (or equal to) 100.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning documents to check the price field.
- 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 time to find matching documents grows in a straight line as the collection gets bigger.
[X] Wrong: "Using $gt or $gte always makes queries fast because they are simple comparisons."
[OK] Correct: Without an index on the field, MongoDB must check every document, so the query time grows with collection size.
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
$gt operator do in a MongoDB query?Solution
Step 1: Understand the meaning of
The$gt$gtoperator means "greater than" and selects values strictly larger than the given number.Step 2: Compare with other operators
$gtemeans "greater than or equal to", so it includes the number itself, unlike$gt.Final Answer:
Finds documents where the field value is strictly greater than the specified value. -> Option CQuick Check:
$gt= strictly greater than [OK]
- Confusing $gt with $gte
- Thinking $gt includes equal values
- Mixing $gt with less than operators
age is greater than or equal to 18 in MongoDB?Solution
Step 1: Check the operator and syntax
The correct operator for "greater than or equal to" is$gte, and it must be used as{ field: { $gte: value } }.Step 2: Validate the value type and format
The value should be a number (18), not a string or invalid syntax like> 18.Final Answer:
{ age: { $gte: 18 } } -> Option AQuick Check:
Correct syntax uses $gte with number [OK]
- Using quotes around numbers
- Writing invalid syntax like $gte: > 18
- Confusing $gt and $gte
products with documents:{ "name": "Pen", "price": 5 }{ "name": "Notebook", "price": 10 }{ "name": "Backpack", "price": 20 }What will be the result of the query
db.products.find({ price: { $gt: 10 } })?Solution
Step 1: Understand the query condition
The query uses$gt: 10, so it selects documents wherepriceis strictly greater than 10.Step 2: Check each document's price
"Pen" has price 5 (not > 10), "Notebook" has price 10 (not > 10), "Backpack" has price 20 (greater than 10).Final Answer:
[{ "name": "Backpack", "price": 20 }] -> Option DQuick Check:
Only price > 10 matches [OK]
- Including documents with price equal to 10
- Confusing $gt with $gte
- Selecting documents with price less than 10
db.users.find({ age: { $gte: 21 } }) but it returns no results even though some users are 21 or older. What is the likely problem?Solution
Step 1: Check the query syntax
The syntax{ age: { $gte: 21 } }is correct and supported by MongoDB.Step 2: Consider data issues
If no results appear, likely the fieldageis misspelled or missing in documents, so no matches occur.Final Answer:
The field nameageis misspelled in the documents. -> Option BQuick Check:
Field name mismatch causes no results [OK]
- Assuming $gte is unsupported
- Switching $gte to $gt unnecessarily
- Ignoring possible typos in field names
total amount greater than or equal to 100 but less than 200. Which MongoDB query correctly uses $gte and $gt to achieve this?Solution
Step 1: Understand the range conditions
You want totals >= 100 and < 200, so use$gte: 100and$lt: 200(less than 200).Step 2: Check each option's operators
{ total: { $gte: 100, $lt: 200 } } correctly uses$gte: 100and$lt: 200. Other options misuse operators or values.Final Answer:
{ total: { $gte: 100, $lt: 200 } } -> Option AQuick Check:
Use $gte for lower bound, $lt for upper bound [OK]
- Using $gt instead of $gte for lower bound
- Using $gte for upper bound instead of $lt
- Mixing operator directions incorrectly
