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

CategoryComparison
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ <field>: { $gt: <value> } }

⚙️ Parameters

fieldThe document field to match; dotted paths access nested fields.
valueThe 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏