Introduction
These operators help you find data smaller than a certain value. They make searching easier and faster.
Jump into concepts and practice - no test required
These operators help you find data smaller than a certain value. They make searching easier and faster.
{ field: { $lt: value } } // less than
{ field: { $lte: value } } // less than or equal to$lt means strictly less than the value.
$lte means less than or exactly equal to the value.
{ age: { $lt: 30 } }{ price: { $lte: 100 } }{ date: { $lt: new ISODate("2024-01-01") } }This example adds some products with prices. Then it finds all products with price less than 20.
db.products.insertMany([
{ name: "Pen", price: 5 },
{ name: "Notebook", price: 15 },
{ name: "Backpack", price: 45 },
{ name: "Calculator", price: 100 }
])
// Find products cheaper than 20
const cheapProducts = db.products.find({ price: { $lt: 20 } }).toArray()
printjson(cheapProducts)Remember $lt excludes the value itself, $lte includes it.
These operators work with numbers, dates, and strings (alphabetical order).
Use them inside a query object to filter results easily.
$lt means less than, $lte means less than or equal.
Use them to find smaller values in your data.
They help make your searches precise and simple.
$lt do in a query?$lt$lt operator means "less than" in MongoDB queries.$lt means less than [OK]age is less than or equal to 30?$lte means "less than or equal to" in MongoDB.age is less than or equal to 30 is { age: { $lte: 30 } }.$lte means less than or equal [OK]products with documents: { "name": "Pen", "price": 5 }{ "name": "Notebook", "price": 10 }{ "name": "Bag", "price": 20 }db.products.find({ price: { $lt: 10 } })?$lt: 10, so it finds documents where price is less than 10.score is less than or equal to 50:db.scores.find({ score: { $lt: 50 } })score is exactly 50. What is the error and how to fix it?$lt, which excludes values equal to 50.$lte (less than or equal).$lte instead of $lt to include 50 -> Option D$lte includes equal values [OK]orders with documents containing total values. You want to find all orders with total less than or equal to 100 but greater than 50. Which query correctly uses $lt and $lte to achieve this?$gt: 50 means strictly greater than 50, $lte: 100 means less than or equal to 100.{ total: { $gt: 50, $lte: 100 } }.