MongoDB $gt Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Comparison

What You’ll Learn

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.

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.”

📝 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:

$match{ score: { $gt: 80 } } (filters documents)
$project{ $gt: [ "$salary", 50000 ] } (returns boolean)
$cond → use expression form as the if condition

⚡ Quick Reference

QuestionAnswer
Operator typeComparison operator (query + expression)
Query syntax{ field: { $gt: value } }
Expression syntax{ $gt: [ expr1, expr2 ] }
Includes equals?No — use $gte for ≥
Expression outputtrue or false
Common stages$match, $project, $cond
$match filter
{
  score: { $gt: 80 }
}

Scores above 80

Expression
{
  $gt: [
    "$salary",
    50000
  ]
}

Returns true/false

Field vs field
{
  $gt: [
    "$spent",
    "$budget"
  ]
}

Over budget check

find()
db.products.find({
  price: { $gt: 100 }
})

Works in find too

Examples Gallery

Walk through sample student data, filter high scores with $match, and compare fields with the expression form.

📚 Filter High Scores

Use a students collection and find everyone who scored above 80 with $match.

Sample Input Documents

Suppose you have a students collection with name and score fields:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Alice", "score": 85 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Bob", "score": 70 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Charlie", "score": 95 }
]

Example 1 — $gt in a $match Stage

Find students who scored more than 80:

mongosh
db.students.aggregate([
  {
    $match: {
      score: { $gt: 80 }
    }
  }
])

How It Works

  • Alice (85) and Charlie (95) are greater than 80, so they pass the filter.
  • Bob (70) is excluded because 70 is not greater than 80.
  • A score of exactly 80 would also be excluded — use $gte to include it.

📈 Practical Patterns

Use the expression form in find(), add boolean flags, and power conditional logic.

Example 2 — $gt in a find() Query

The same query form works outside aggregation pipelines:

mongosh
db.products.find({
  price: { $gt: 100 }
})

How It Works

$gt in find() uses the same query form as $match. It is one of the most common ways to filter numeric and date fields in MongoDB.

Example 3 — Add a Boolean Field with $project

Flag employees earning more than 50,000 on every document:

mongosh
db.employees.aggregate([
  {
    $project: {
      name: 1,
      salary: 1,
      isHighEarner: {
        $gt: [ "$salary", 50000 ]
      }
    }
  }
])

How It Works

The expression form does not filter documents. It adds true or false to every document based on whether salary exceeds 50,000.

Example 4 — $gt Inside $cond

Assign a performance label based on score:

mongosh
db.students.aggregate([
  {
    $project: {
      name: 1,
      score: 1,
      performance: {
        $cond: [
          { $gt: [ "$score", 80 ] },
          "Excellent",
          "Needs Improvement"
        ]
      }
    }
  }
])

How It Works

When $gt returns true (score > 80), $cond outputs "Excellent". Otherwise it outputs "Needs Improvement". Pair $gt with $and for multi-range grading.

🚀 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.

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.

💡 Best Practices

✅ Do

  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about $gt

Use these points when writing greater-than comparisons in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $gte for greater-than-or-equal comparisons, or review $eq for equality checks.

Next: $gte →

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.

5 people found this page helpful