$gt (Greater Than)
Last updated: 2026-10-09
$gt (Greater Than)
$gt selects documents where a field value is strictly greater than the specified value. `{ age: { $gt: 21 } }` matches users older than 21, excluding exactly 21. It is used for range queries: price floors, date filters, score thresholds. Unlike $gte it excludes the boundary value. Strings compare lexicographically and dates chronologically.
| Category | Comparison |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $gt: <value> } }
⚙️ Parameters
| field | The document field to match; dotted paths access nested fields. |
|---|---|
| value | The comparison boundary value; the boundary itself is excluded. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $gt: find users older than 21 (21 excluded)
db.users.find({ age: { $gt: 21 } })
// Price floor + date range
db.products.find({ price: { $gt: 100 } })
db.orders.find({ createdAt: { $gt: new Date('2026-09-01') } })
Output:
// Returns users with age above 21, products priced above 100, and orders after Sep 1
📌 Notes
- $gt excludes the boundary; use $gte to include it
- Combine $gt and $lt on one field for an open interval, e.g. { price: { $gt: 10, $lt: 20 } }
- Numbers and strings do not coerce: $gt: 10 will not match the string '10'
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $gt and $gte?
A$gt is strictly greater (excludes the boundary); $gte includes it. Use $gt: 21 for older than 21 and $gte: 21 for at least 21.
QCan $gt compare strings or dates?
AYes. Strings compare lexicographically; wrap dates in new Date() for chronological comparison, otherwise string dates compare as text.
QHow do I query price between 10 and 20?
AUse a compound condition on one field: { price: { $gt: 10, $lt: 20 } }, an open interval.