The $gt operator checks whether one value is greater than another. Use it in $match to filter documents above a threshold, or in expression stages like $project and $cond to compare fields and get a true/false result.
01
Greater Than
Left value > right value.
02
Two Syntax Forms
Query filter vs expression.
03
$match Stage
Filter above threshold.
04
Boolean Output
true or false in $project.
05
Use Cases
Scores, prices, dates.
06
No Shorthand
Must use explicit $gt.
Fundamentals
Definition and Usage
In MongoDB, the $gt operator tests whether one value is strictly greater than another. In a $match stage, { score: { $gt: 80 } } filters documents where score is greater than 80. In an aggregation expression, { $gt: [ "$salary", 50000 ] } returns true or false for each document. It is one of the core comparison operators alongside $gte, $lt, and $lte.
💡
Beginner Tip
Unlike $eq, there is no shorthand for $gt. You must always write { score: { $gt: 80 } } explicitly. Values equal to the threshold are not included — use $gte for “greater than or equal.”
Foundation
📝 Syntax
$gt has two forms depending on where you use it:
Query form (in $match or find filters)
mongosh
{ field: { $gt: <value> } }
Aggregation expression form (in $project, $cond, etc.)
mongosh
{ $gt: [ <expression1>, <expression2> ] }
Syntax Rules
Query form — filters documents where the field is greater than the given value.
Expression form — compares two expressions and returns true or false.
Strictly greater than — equal values return false (use $gte to include equals).
Works with numbers, dates, and strings (BSON comparison order).
Use expression form inside $project, $addFields, $cond, and $filter.
💡 Query Form vs Expression Form
Know which form to use based on your pipeline stage:
When $gt returns true (score > 80), $cond outputs "Excellent". Otherwise it outputs "Needs Improvement". Pair $gt with $and for multi-range grading.
Applications
🚀 Use Cases
Performance evaluation — find students, employees, or products above a score or price threshold.
Threshold monitoring — detect values exceeding limits in system metrics or quality control.
Date filtering — select records created after a specific date using { createdAt: { $gt: date } }.
Conditional logic — use as the if condition inside $cond for tiered labels and bonuses.
🧠 How $gt Works
1
MongoDB reads the operands
In $match, it checks a field against a threshold. In expressions, it evaluates two operands like "$score" and 80.
Input
2
$gt compares values
MongoDB checks whether the first value is strictly greater than the second using BSON comparison rules.
Compare
3
Result is applied in the pipeline
In $match, matching documents pass through. In expressions, true or false is stored in the output field.
Output
=
🔍
Filtered or flagged data
You get documents above a threshold or boolean fields ready for conditional logic.
Wrap Up
Conclusion
The $gt operator is a core comparison tool in MongoDB. It powers threshold filtering in $match and find(), boolean field creation in $project, and conditional branching in $cond — making it essential for range-based queries.
For beginners, remember two forms: query form { field: { $gt: value } } filters documents, and expression form { $gt: [ expr1, expr2 ] } returns true or false. Use $gte when you need to include values equal to the threshold.
Use query form in $match and find() for threshold filtering
Use expression form in $project and $cond
Use $gte when equal values should be included
Combine with $and and $lt for range queries
Index fields used in $gt filters for better performance
❌ Don’t
Expect values equal to the threshold to match (use $gte)
Use expression form inside $match without $expr
Compare different BSON types and expect reliable results
Confuse $gt with $gte or $lt
Use $gt when you need exact equality (use $eq)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $gt
Use these points when writing greater-than comparisons in MongoDB.
5
Core concepts
🔢01
Greater Than
Strictly above threshold.
Purpose
📝02
Two Forms
Query filter vs expression.
Syntax
🛠03
$match Filter
Above-threshold docs.
Usage
🔍04
$cond Ready
Boolean for if-then-else.
Use case
⚠05
Not $gte
Equals excluded.
Edge case
❓ Frequently Asked Questions
$gt checks whether one value is greater than another. In query filters it matches documents where a field is greater than a threshold. In aggregation expressions it returns true or false when comparing two expressions.
Query form: { field: { $gt: value } }. Aggregation expression form: { $gt: [ <expression1>, <expression2> ] }. Unlike $eq, there is no shorthand — you must use the explicit $gt operator.
In $match, $gt filters documents and keeps only those where the field exceeds the threshold. In $project or $addFields, $gt compares two expressions and adds a boolean field (true/false) to each document.
Yes. In aggregation expressions use { $gt: [ "$spent", "$budget" ] } to test whether one field is greater than another on each document.
Yes. $gt uses BSON comparison order. Dates compare chronologically, numbers numerically, and strings lexicographically. Ensure both operands are the same type for predictable results.