Introduction
These operators help you find data where a value is bigger than a number. They make searching easier and faster.
Jump into concepts and practice - no test required
These operators help you find data where a value is bigger than a number. They make searching easier and faster.
{ field: { $gt: value } } // for greater than
{ field: { $gte: value } } // for greater than or equal to$gt means 'greater than' and excludes the value itself.
$gte means 'greater than or equal to' and includes the value.
{ age: { $gt: 18 } }{ price: { $gte: 100 } }{ score: { $gt: 75 } }This example adds some products with prices. Then it finds products costing more than 20.
db.products.insertMany([
{ name: "Pen", price: 5 },
{ name: "Notebook", price: 15 },
{ name: "Backpack", price: 50 },
{ name: "Laptop", price: 1000 }
])
// Find products with price greater than 20
const expensiveProducts = db.products.find({ price: { $gt: 20 } }).toArray()
expensiveProductsRemember $gt excludes the value itself, so price $20 won't show in the example above.
Use $gte if you want to include the value itself.
$gt finds values strictly greater than a number.
$gte finds values greater than or equal to a number.
They help filter data easily in MongoDB queries.
$gt operator do in a MongoDB query?$gt$gt operator means "greater than" and selects values strictly larger than the given number.$gte means "greater than or equal to", so it includes the number itself, unlike $gt.$gt = strictly greater than [OK]age is greater than or equal to 18 in MongoDB?$gte, and it must be used as { field: { $gte: value } }.> 18.products with documents:{ "name": "Pen", "price": 5 }{ "name": "Notebook", "price": 10 }{ "name": "Backpack", "price": 20 }db.products.find({ price: { $gt: 10 } })?$gt: 10, so it selects documents where price is strictly greater than 10.db.users.find({ age: { $gte: 21 } }) but it returns no results even though some users are 21 or older. What is the likely problem?{ age: { $gte: 21 } } is correct and supported by MongoDB.age is misspelled or missing in documents, so no matches occur.age is misspelled in the documents. -> Option Btotal amount greater than or equal to 100 but less than 200. Which MongoDB query correctly uses $gte and $gt to achieve this?$gte: 100 and $lt: 200 (less than 200).$gte: 100 and $lt: 200. Other options misuse operators or values.