Sling Academy
Home/MongoDB/Using $lt and $lte operators in MongoDB (with examples)

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

Last updated: February 02, 2024

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.

Next Article: Using $in and $nin operators in MongoDB (with examples)

Previous Article: MongoDB: Using $gt and $gte operators (with examples)

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)