MongoDB $gt Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $gt?

$gt means greater than. Use it to filter documents where a field exceeds a value, or in aggregation to return true / false from a comparison. Equality does not match—use $gte for that.

Remember
$gt = greater than (strict)

Query filter:
  { score: { $gt: 80 } }

Aggregation expression:
  { $gt: [ "$score", 80 ] }  → true / false

85 > 80 → true (kept / true)
80 > 80 → false (excluded / false)
72 > 80 → false

sibling: $gte includes equals
UsePrefer
Strictly greater than$gt
Greater than or equal$gte
Less than$lt

How to Use $gt in MongoDB

Pick filter mode (keep documents) or expression mode (boolean field).

  1. For filtering: use find() or $match with { field: { $gt: value } }.
  2. For a boolean: use $addFields / $project with { $gt: [ expr1, expr2 ] }.
  3. To compare two fields in a filter, wrap with $expr.
  4. Use $gte when the boundary value should also match.
PieceRequired?Role
Query form—{ field: { $gt: value } } filters docs
Expression form—{ $gt: [ a, b ] } → boolean
OperandsYesSame BSON type when possible

Pseudocode

Pseudocode
// Filter mode
for each document:
    if field > threshold:
        keep document
    else:
        drop document

// Expression mode
for each document:
    result = (expr1 > expr2)   // true or false
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing greater-than checks.

TopicDetail
MeaningGreater than (excludes equals)
Query syntax{ field: { $gt: value } }
Agg syntax{ $gt: [ <a>, <b> ] }
vs $gte$gte includes equality
Two fields in findUse $expr + $gt
TypesNumbers, dates, strings (BSON order)
Sample checkscore: { $gt: 80 } keeps 85, drops 80

Live Preview

Watch $gt test whether a score is greater than 80.

Simulates { score: { $gt: 80 } } / { $gt: [ score, 80 ] }.

Result
score = 85
threshold = 80
$gt → true (kept / passed)

Worked Walkthrough — Passing Scores

Keep students whose score is greater than 80.

Calculation
Documents:
  { name: "Asha", score: 85 }
  { name: "Ben",  score: 72 }

Test: score > 80

Asha: 85 > 80 → true  → keep
Ben:  72 > 80 → false → drop

Result: only Asha

MongoDB Examples

We use two students. First filter with a query, then add a boolean field in aggregation. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine two students: Asha scored 85 and Ben scored 72. We save both in a collection named students.

mongosh
db.students.insertMany([
  { _id: 1, name: "Asha", score: 85 },
  { _id: 2, name: "Ben", score: 72 }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps two documents in students. Run find() to see them:

mongosh
db.students.find()

Read those documents like this:

Field in MongoDBAshaBen
_id12
name"Asha""Ben"
score8572
Picture
Threshold line at 80:

  Ben 72 ........ 80 ........ Asha 85
                 |
            score > 80 ?

  Ben  → no
  Asha → yes

How It Works

  1. Insert once. insertMany adds two student documents to students.
  2. Scores are numbers. Ready for numeric comparison with $gt.
  3. find() shows both rows. The next example keeps only the ones above 80.

Example 2 — Filter with $gt

Goal: return only students whose score is greater than 80.

mongosh
db.students.find({
  score: { $gt: 80 }
});

How It Works

  1. Apply a filter. { score: { $gt: 80 } } means “score must be > 80”.
  2. Test each document. Asha 85 passes; Ben 72 fails.
  3. Only matches remain. The result set is just Asha’s document.

Example 3 — Boolean Expression

Same students, but keep every document and add a passed boolean with the aggregation form of $gt.

mongosh
db.students.aggregate([
  {
    $addFields: {
      passed: {
        $gt: ["$score", 80]
      }
    }
  }
]);

How It Works

  1. Same stored documents. Still using Asha and Ben from Example 1.
  2. Compare as an expression. { $gt: ["$score", 80] } returns a boolean per document.
  3. Nothing is dropped. Both rows stay; only the new passed flag differs.

Edge Cases & Pitfalls

Check these before using $gt in production queries.

equals

Equals do not match

{ score: { $gt: 80 } } excludes 80. Use $gte when the boundary should pass.

types

Type mismatches surprise

Comparing a number to a string uses BSON type order, not numeric math. Keep types consistent.

fields

Two fields need $expr in find

To filter with spent > budget, use $expr: { $expr: { $gt: ["$spent", "$budget"] } }.

index

Range filters love indexes

An index on the compared field helps $gt / $gte range scans stay fast on large collections.

Performance Notes

ApproachCostBest for
Indexed $gt filterRange scanLarge collections with a field index
Unindexed $gtCollection scanSmall data or rare queries
Agg boolean $gtPer-document compareFlags without dropping rows

Put selective $gt filters early in $match so later stages see fewer documents.

Key Takeaways

  • Role: strict greater-than comparison.
  • Filter: { field: { $gt: value } }.
  • Expression: { $gt: [ a, b ] } → boolean.
  • Equals: use $gte when the boundary should match.

One line: $gt answers “is this value strictly greater than that?”—as a filter or as a boolean.

Frequently Asked Questions

$gt means greater than. In a query filter it keeps documents where a field is greater than a value. In aggregation expressions it compares two expressions and returns true or false.
Query form: { field: { $gt: value } }. Aggregation form: { $gt: [ <expression1>, <expression2> ] }. There is no shorthand like { field: value } for greater-than.
$gt is strict greater than (excludes equality). $gte is greater than or equal (includes equality).
Yes in aggregation: { $gt: [ "$spent", "$budget" ] }. In a find filter, compare two fields with $expr and $gt.
Yes. Numbers compare numerically, dates chronologically, and strings lexicographically. Keep operand types consistent for predictable results.
In find()/ $match filters to select documents, and in expression stages like $project and $addFields to create boolean fields.

Next: $gte

Include the boundary value with greater-than-or-equal comparisons.

$gte tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful