MongoDB $gte Operator

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

What Is $gte?

$gte means greater than or equal to (≥). In aggregation it returns a boolean. In queries / $match it keeps documents that meet or exceed a threshold.

Remember
$gte = ≥  (greater than or equal)

Aggregation:
{ $gte: [ <a>, <b> ] }   → true if a ≥ b

Query / $match:
{ field: { $gte: value } }

includes equality  → yes (250 ≥ 250)
vs $gt             → $gt excludes equals
UsePrefer
≥ threshold (includes equal)$gte
> threshold (excludes equal)$gt
≤ threshold$lte

How to Use $gte in MongoDB

Pick the mode that matches your goal: compute a boolean, or filter documents.

  1. Decide the left value (often a field) and the right threshold.
  2. For a boolean flag, use { $gte: [ "$field", value ] } in $addFields or $project.
  3. To filter rows, use { field: { $gte: value } } in $match (or find()).
  4. For field-to-field expression compares inside $match, wrap with $expr.
PieceRequired?Role
$gteYesGreater-than-or-equal comparison
Left valueYesField or expression being tested
Right valueYesThreshold or other expression

Pseudocode

Pseudocode
for each document:
    a = evaluate(left)
    b = evaluate(right)
    result = (a >= b)   // true or false

    // aggregation: emit boolean
    // $match: keep document only if result is true

Cheat Sheet

Keep these facts handy while writing ≥ comparisons.

TopicDetail
TypeComparison operator (query + aggregation expression)
Aggregation{ $gte: [ "$qty", 250 ] }
Query / $match{ qty: { $gte: 250 } }
Includes equalYes — 250 ≥ 250 → true
vs $gt$gt excludes equality
Dates / stringsSupported (same type preferred)
With $condCommon: if: { $gte: [...] }

Live Preview

Simulate $gte: ["$qty", 250]. Change quantity and watch true / false (equality included).

Same idea as the mongosh examples: qty must be greater than or equal to 250.

Result
qty = 250
$gte(qty, 250) → true

{ qtyGte250: true }

Worked Walkthrough — qty ≥ 250

Rule: a row passes when quantity is at least 250. Trace a few values, including the equal case.

Calculation
Rule:
  $gte(qty, 250)  →  qty ≥ 250

qty = 300  →  300 ≥ 250  → true
qty = 250  →  250 ≥ 250  → true   ← equal counts
qty = 200  →  200 ≥ 250  → false
qty = 180  →  180 ≥ 250  → false

Compare with $gt:
  $gt(250, 250) → false  (strict)

MongoDB Examples

We use a small inventory collection. First we compute a boolean flag, then we filter with $match. Insert the sample data once in mongosh.

Example 1 — Sample Data

Imagine five inventory items. Each document has an item code and a quantity. We save them in a collection named inventory.

mongosh
db.inventory.insertMany([
  { _id: 1, item: "abc1", qty: 300 },
  { _id: 2, item: "abc2", qty: 200 },
  { _id: 3, item: "xyz1", qty: 250 },
  { _id: 4, item: "VWZ1", qty: 300 },
  { _id: 5, item: "VWZ2", qty: 180 }
]);

How the Data Is Stored in MongoDB

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

mongosh
db.inventory.find()

Read those documents like this:

Field in MongoDBExamplePlain English
_id1 … 5Document id for this lesson
item"abc1"Product / SKU code
qty250Quantity on hand
Picture
Threshold: qty ≥ 250

          |---- pass (≥) ----|
  --------+------------------+--------
        250

  180, 200 → false
  250, 300 → true   ← 250 is included

How It Works

  1. Insert once. insertMany adds five inventory documents.
  2. MongoDB stores fields as written. Each row keeps item and qty as plain values.
  3. find() shows the same shape. What you inserted is what you get back—ready for $gte next.

Example 2 — Boolean Flag with $gte

Goal: add qtyGte250: true/false without removing documents.

mongosh
db.inventory.aggregate([
  {
    $addFields: {
      qtyGte250: { $gte: ["$qty", 250] }
    }
  }
]);

How It Works

  1. Read qty. For each stored document, MongoDB evaluates "$qty".
  2. Compare to 250. $gte: ["$qty", 250] asks “is qty ≥ 250?”
  3. Equal counts. Document 3 (qty: 250) is true.
  4. Keep all rows. $addFields only adds the boolean flag.

Example 3 — Filter with $match

Same rule, but keep only documents where qty is at least 250. The field form is the usual query style.

mongosh
db.inventory.aggregate([
  {
    $match: {
      qty: { $gte: 250 }
    }
  }
]);

How It Works

  1. Same stored documents. Still using the five rows from Example 1.
  2. Same ≥ 250 rule. 200 and 180 drop out; 250 stays.
  3. Field form is index-friendly. { qty: { $gte: 250 } } is the common filter style.
  4. Filter, don’t flag. Only the three passing documents remain.

Edge Cases & Pitfalls

Check these before shipping $gte logic.

equal

Equality is included

If you meant “strictly above,” use $gt. With $gte, the threshold itself passes.

types

Watch mixed BSON types

Comparing a number to a string uses type order, not numeric order. Convert first when data types may differ.

null

Missing fields usually fail filters

In $match, documents without the field typically do not match { field: { $gte: n } }. Decide if that is what you want.

syntax

Expression form needs an array

In aggregation expressions write { $gte: ["$qty", 250] }—two arguments in an array.

Performance Notes

ApproachCostBest for
$match field formCan use indexesEarly filtering on stored fields
$expr + $gteExpression eval per docField-to-field comparisons
$gte in $addFieldsPer-document booleanFlags / reports without dropping rows

Put selective filters first with $match so later stages process fewer documents. Prefer indexed query predicates when you do not need $expr.

Key Takeaways

  • Role: greater than or equal (≥).
  • Modes: boolean in aggregation, or filter in $match.
  • Equality: included — that is the difference from $gt.
  • Types: keep operands the same BSON type when possible.

One line: $gte answers “is this ≥ that?”—including the equal case.

Frequently Asked Questions

$gte checks whether one value is greater than or equal to another. In aggregation it returns true or false. In queries and $match it keeps documents that meet or exceed a threshold.
Aggregation: { $gte: [ <expression1>, <expression2> ] }. Query / $match field form: { field: { $gte: value } }.
$gte includes equality. $gt is strictly greater than. So qty >= 250 matches 250; qty > 250 does not.
Yes. Use the field form { qty: { $gte: 250 } } for simple filters, or wrap the expression form in $expr when you need aggregation operators.
Yes. Numbers compare numerically, dates chronologically, and strings lexicographically. Prefer the same BSON type on both sides for predictable results.
Aggregation $gte compares value and type using BSON comparison order. Mixed types can surprise you—normalize types when you can.

Next: $hour

Get the hour portion (0–23) from a date value.

$hour 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