Using $lt and $lte operators in MongoDB (with examples)

Updated: February 2, 2024 By: Guest Contributor Post a comment

Introduction to MongoDB’s $lt and $lte

MongoDB provides a rich set of query operators that you can leverage when building queries for your application. Among these operators are the comparison operators $lt (less than) and $lte (less than or equal to). These operators allow you to perform queries that compare the value of a field against a specified value. In this tutorial, we’ll dive into understanding and using these operators with practical examples.

Basics of $lt and $lte

The $lt operator matches documents where the value of the field is less than the specified value. On the other hand, the $lte operator matches documents where the field value is less than or equal to the specified value.

db.collection.find({ field: { $lt: value } });
db.collection.find({ field: { $lte: value } });

Example 1: Using $lt Operator

Let’s consider a collection inventory that includes data about store items. Here’s how you would find items priced less than $20:

db.inventory.find({ price: { $lt: 20 } });

Output:

[
  {
    "_id": ObjectId("..."),
    "item": "journal",
    "price": 10,
    "qty": 25
  },
  {
    "_id": ObjectId("..."),
    "item": "notebook",
    "price": 13,
    "qty": 50
  },
  // Add more objects as needed
]

Example 2: Using $lte Operator

To find items in the same collection priced at $20 or less, use the $lte operator:

db.inventory.find({ price: { $lte: 20 } });

Output:

[
  {
    "_id": ObjectId("..."),
    "item": "journal",
    "price": 10,
    "qty": 25
  },
  {
    "_id": ObjectId("..."),
    "item": "notebook",
    "price": 13,
    "qty": 50
  },
  {
    "_id": ObjectId("..."),
    "item": "planner",
    "price": 20,
    "qty": 75
  },
  // Add more objects as needed
]

Advanced $lt and $lte Examples

We can apply these operators not only with numbers but also with dates and even within subdocuments or arrays.

Example 3: Comparing Dates

To find documents where the date field is before a particular date, use the $lt operator with a Date object. If you want to include the specified date, use $lte:

db.events.find({ eventDate: { $lt: new Date('2023-01-01') } });
db.events.find({ eventDate: { $lte: new Date('2023-01-01') } });

Example 4: Queries on Subdocuments

you can also use $lt and $lte within subdocuments. For instance, if the inventory items’ sizes are stored in a subdocument:

db.inventory.find({ "size.height": { $lt: 15 } });

Output:

[
  {
    "_id": ObjectId("..."),
    "item": "desk",
    "size": {
      "width": 24,
      "height": 12
    }
  },
  // Add more objects as needed
]

Example 5: Using in Array Fields

When querying array fields, $lt and $lte match any array element that fulfills the condition:

db.inventory.find({ tags: { $lte: "C" } });

Output:

[
  {
    "_id": ObjectId("..."),
    "item": "stapler",
    "tags": ["A", "B"]
  },
  // Add more objects as needed
]

Combining $lt and $lte with Other Operators

It’s common to combine $lt and $lte with other operators such as $gt (greater than) or $gte (greater than or equal to) to form range queries.

Example 6: Range Query

Here’s how to find items priced between $10 and $20, inclusively:

db.inventory.find({ price: { $gte: 10, $lte: 20 } });

Output:

[
  {
    "_id": ObjectId("..."),
    "item": "journal",
    "price": 10
  },
  {
    "_id": ObjectId("..."),
    "item": "notebook",
    "price": 13
  },
  // Add more objects as needed
]

Index Utilization

Using $lt and $lte with indexed fields can significantly improve the performance of your queries. Ensurethat your queries are backed by appropriate indexes whenever possible. This can be particularly important when working with large datasets or in production environments.

Conclusion

In conclusion, understanding and effectively using the $lt and $lte operators can greatly enhance the power and precision of your MongoDB queries, allowing for flexible comparisons across a variety of field types. By personalizing your queries to your application’s needs and combining these operators with others, you’ll be able to efficiently retrieve the data you require.