Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Find value above a specific value in MongoDB documents?
To find values above a specific value in MongoDB, use the $gte (greater than or equal to) operator to match documents where a field's value meets or exceeds the specified threshold.
Syntax
db.collectionName.find({
fieldName: { $gte: value }
});
Create Sample Data
Let us create a collection with price documents ?
db.demo571.insertMany([
{ "Price": 140 },
{ "Price": 100 },
{ "Price": 110 },
{ "Price": 240 }
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5e909b3439cfeaaf0b97b587"),
ObjectId("5e909b3639cfeaaf0b97b588"),
ObjectId("5e909b3839cfeaaf0b97b589"),
ObjectId("5e909b3c39cfeaaf0b97b58a")
]
}
Display all documents from the collection ?
db.demo571.find();
{ "_id": ObjectId("5e909b3439cfeaaf0b97b587"), "Price": 140 }
{ "_id": ObjectId("5e909b3639cfeaaf0b97b588"), "Price": 100 }
{ "_id": ObjectId("5e909b3839cfeaaf0b97b589"), "Price": 110 }
{ "_id": ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price": 240 }
Example: Find Prices ? 140
Following is the query to get documents with prices above or equal to 140 ?
db.demo571.find({
Price: { $gte: 140 }
});
{ "_id": ObjectId("5e909b3439cfeaaf0b97b587"), "Price": 140 }
{ "_id": ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price": 240 }
Key Points
-
$gteincludes the specified value (greater than or equal to). - For strictly greater than, use
$gtinstead of$gte. - Works with numbers, dates, and strings (lexicographic comparison).
Conclusion
The $gte operator efficiently filters MongoDB documents based on minimum value thresholds. Use $gt for strictly greater than comparisons when you need to exclude the boundary value.
Advertisements
