Bird
Raised Fist0
MongoDBquery~10 mins

$gt and $gte for greater 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 - $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.

Practice

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

Solution

  1. Step 1: Understand the meaning of $gt

    The $gt operator means "greater than" and selects values strictly larger than the given number.
  2. Step 2: Compare with other operators

    $gte means "greater than or equal to", so it includes the number itself, unlike $gt.
  3. Final Answer:

    Finds documents where the field value is strictly greater than the specified value. -> Option C
  4. Quick Check:

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

Solution

  1. 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 } }.
  2. Step 2: Validate the value type and format

    The value should be a number (18), not a string or invalid syntax like > 18.
  3. Final Answer:

    { age: { $gte: 18 } } -> Option A
  4. Quick Check:

    Correct syntax uses $gte with number [OK]
Hint: Use { field: { $gte: number } } for greater or equal [OK]
Common Mistakes:
  • Using quotes around numbers
  • Writing invalid syntax like $gte: > 18
  • Confusing $gt and $gte
3. Given the collection 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 } })?
medium
A. [{ "name": "Notebook", "price": 10 }, { "name": "Backpack", "price": 20 }]
B. [{ "name": "Pen", "price": 5 }]
C. []
D. [{ "name": "Backpack", "price": 20 }]

Solution

  1. Step 1: Understand the query condition

    The query uses $gt: 10, so it selects documents where price is strictly greater than 10.
  2. 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).
  3. Final Answer:

    [{ "name": "Backpack", "price": 20 }] -> Option D
  4. Quick Check:

    Only price > 10 matches [OK]
Hint: Remember $gt excludes equal values [OK]
Common Mistakes:
  • Including documents with price equal to 10
  • Confusing $gt with $gte
  • Selecting documents with price less than 10
4. You wrote the query db.users.find({ age: { $gte: 21 } }) but it returns no results even though some users are 21 or older. What is the likely problem?
medium
A. You should use $gt instead of $gte.
B. The field name age is misspelled in the documents.
C. The query syntax is incorrect; $gte cannot be used here.
D. MongoDB does not support $gte operator.

Solution

  1. Step 1: Check the query syntax

    The syntax { age: { $gte: 21 } } is correct and supported by MongoDB.
  2. Step 2: Consider data issues

    If no results appear, likely the field age is misspelled or missing in documents, so no matches occur.
  3. Final Answer:

    The field name age is misspelled in the documents. -> Option B
  4. Quick Check:

    Field name mismatch causes no results [OK]
Hint: Check field names carefully if no results [OK]
Common Mistakes:
  • Assuming $gte is unsupported
  • Switching $gte to $gt unnecessarily
  • Ignoring possible typos in field names
5. You want to find all orders with a total amount greater than or equal to 100 but less than 200. Which MongoDB query correctly uses $gte and $gt to achieve this?
hard
A. { total: { $gte: 100, $lt: 200 } }
B. { total: { $gt: 100, $gte: 200 } }
C. { total: { $gte: 100, $gt: 200 } }
D. { total: { $gte: 100, $gt: 199 } }

Solution

  1. Step 1: Understand the range conditions

    You want totals >= 100 and < 200, so use $gte: 100 and $lt: 200 (less than 200).
  2. Step 2: Check each option's operators

    { total: { $gte: 100, $lt: 200 } } correctly uses $gte: 100 and $lt: 200. Other options misuse operators or values.
  3. Final Answer:

    { total: { $gte: 100, $lt: 200 } } -> Option A
  4. Quick Check:

    Use $gte for lower bound, $lt for upper bound [OK]
Hint: Use $gte for start, $lt for end of range [OK]
Common Mistakes:
  • Using $gt instead of $gte for lower bound
  • Using $gte for upper bound instead of $lt
  • Mixing operator directions incorrectly