0
0
MongoDBquery~5 mins

$lt and $lte for less than in MongoDB

Choose your learning style9 modes available
Introduction

These operators help you find data smaller than a certain value. They make searching easier and faster.

Finding products cheaper than $20 in a store database.
Getting all students with scores less than 50 in a test.
Listing events that happened before a certain date.
Filtering books with fewer than 300 pages.
Finding employees with less than 5 years of experience.
Syntax
MongoDB
{ 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.

Examples
Finds documents where age is less than 30.
MongoDB
{ age: { $lt: 30 } }
Finds documents where price is 100 or less.
MongoDB
{ price: { $lte: 100 } }
Finds documents with dates before January 1, 2024.
MongoDB
{ date: { $lt: new ISODate("2024-01-01") } }
Sample Program

This example adds some products with prices. Then it finds all products with price less than 20.

MongoDB
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)
OutputSuccess
Important Notes

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.

Summary

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