MongoDB - $lt Operator
MongoDB provides powerful query operators to filter and retrieve data efficiently. One such essential operator is the $lt
(less than) operator, which allows users to select documents where a specified field’s value is less than a given value. We can use this operator in methods like, find()
, update()
, etc. as per your requirements.
What is the MongoDB $lt
Operator?
The $lt
operator is a comparison query operator that selects documents where the specified field’s value is strictly less than a given value. It is commonly used in find()
, update()
, and aggregation pipelines to filter data efficiently. This operator supports numerical, date, and array comparisons, making it useful for a wide range of queries. Proper indexing of fields used with $lt
can significantly enhance query performance by reducing scan time and improving retrieval speed.
Key Features of $lt
:
- Used to filter documents based on numerical, date, or array values.
- Supports sorting and range-based queries.
- Compatible with indexing, improving query performance.
- Only compares similar data types (e.g., integer with integer, string with string).
Syntax
{field: {$lt: value}}
Key Terms
field
→ The field name to compare.$lt
→ Specifies the less than condition.value
→ The threshold value to compare against.
Examples of MongoDB $lt Operator
Let’s explore real-world examples of how to use the $lt
operator efficiently. For these examples, we assume the following setup
- Database: GeeksforGeeks
- Collection: employee
- Document: four documents that contain the details of the employees in the form of field-value pairs.

Example 1: Find Employees with Salary Less Than 35,000
In this example, we are selecting those documents where the value of the salary field is less than 35000.
Query:
find({salary: {lt:35000}}).pretty()
Output:

Example 2: Find Employees Younger Than 24 Years
In this example, we are selecting only those documents where the age of the employee is less than 24.
Query:
db.employee.find({ age: { $lt: 24 } }).pretty()
Output

Example 3: Comparing Arrays with $lt
Operator
In this example, we are selecting only those documents where the points array is less than the specified array. MongoDB compares arrays lexicographically (similar to string sorting).
Query:
db.employee.find({ points: { $lt: [50, 70, 90] } }).pretty()
Output

Example 4: Update Salaries for Employees with Less Than 2 Years of Experience
In this example, we are updating the salary of those employees whose experience year is less than 2 years.
Query:
db.employee.updateMany(
{ experience: { $lt: 2 } },
{ $set: { salary: 40000 } }
)
Output

Performance Optimization Tips for Using $lt
To improve query performance when using the $lt
operator, follow these best practices:
1. Use Indexing for Faster Queries
Creating an index on frequently queried fields helps queries run more efficiently. This ensures that queries like { salary: { $lt: 35000 } }
execute faster.
db.employee.createIndex({ salary: 1 })
2. Avoid Large Collection Scans
Queries using $lt
should be optimized with indexes; otherwise, they may cause full collection scans, making queries slower.
3. Use Projection to Limit Retrieved Fields
Fetch only the required fields to reduce query load.
db.employee.find({ salary: { $lt: 35000 } }, { name: 1, salary: 1 })
4. Ensure Proper Data Type Comparisons
MongoDB strictly enforces type comparisons. Always ensure field values are of the correct data type (e.g., avoid comparing strings with numbers).
Key Takeaways About MongoDB $lt Operator
- The
$lt
(less than) operator filters documents where a field’s value is strictly less than a specified value. - Works with numbers, dates, and arrays.
- Can be used inside queries (
find()
), updates (updateMany()
), and aggregations ($match
). - Indexing improves query performance significantly when using
$lt
. - Only compares similar data types, ensuring accurate results
Conclusion
The MongoDB $lt
operator is a powerful tool for filtering and querying documents efficiently. Whether we're filtering employees by salary, identifying recent hires, or updating records based on experience, $lt
provides precise and optimized filtering. By following best practices such as using indexes, limiting fields in queries, and ensuring proper data type usage, developers can significantly boost MongoDB query performance. Mastering the $lt
operator will help us write efficient, scalable, and optimized database queries in MongoDB