Introduction to MongoDB $lte Operator
In MongoDB, the $lte
operator is used to match documents that are less than or equal to a specified value. The $lte
operator can be used for fields of type number, date, and string.
Syntax
The basic syntax for using the $lte
operator in MongoDB is as follows:
{
field: {
$lte: value
}
}
Here, field
is the field to be matched, $lte
is the operator that specifies less than or equal to, and value
is the value to be matched.
Use Cases
The $lte
operator can be used in a variety of scenarios, such as:
- Querying for documents with a numeric field less than or equal to a specified value.
- Querying for documents with a date field earlier than or equal to a specified date.
- Querying for documents with a string field less than or equal to a specified string.
Examples
Example 1: Querying for User Records with Age Less Than or Equal to 30
Assume we have a collection named users
that contains information about many users, including user IDs, names, ages, and so on. Now we need to query for user records with age less than or equal to 30, which can be achieved using the $lte
operator:
db.users.find({ age: { $lte: 30 } })
Assuming the collection contains the following data:
{ "_id": ObjectId("60f72dfe5b99d95eb5ec5f5a"), "name": "Tom", "age": 28 }
{ "_id": ObjectId("60f72e1f5b99d95eb5ec5f5b"), "name": "Jane", "age": 32 }
{ "_id": ObjectId("60f72e295b99d95eb5ec5f5c"), "name": "Mike", "age": 25 }
{ "_id": ObjectId("60f72e2f5b99d95eb5ec5f5d"), "name": "John", "age": 31 }
Running the above query will return the following results:
{ "_id": ObjectId("60f72dfe5b99d95eb5ec5f5a"), "name": "Tom", "age": 28 }
{ "_id": ObjectId("60f72e295b99d95eb5ec5f5c"), "name": "Mike", "age": 25 }
Example 2: Querying for Order Records with Amount Less Than or Equal to 1000
The following example shows how to use the $lte
operator to query for order records with an amount less than or equal to 1000:
db.orders.find({ amount: { $lte: 1000 } })
Assuming the collection contains the following data:
{ "_id" : ObjectId("61fa674a511db968ca810dab"), "order_id" : "20220103001", "order_time" : ISODate("2022-01-03T09:00:00Z"), "status" : "paid", "amount" : 800 }
{ "_id" : ObjectId("61fa674a511db968ca810dac"), "order_id" : "20220103002", "order_time" : ISODate("2022-01-03T10:00:00Z"), "status" : "paid", "amount" : 1500 }
{ "_id" : ObjectId("61fa674a511db968ca810dad"), "order_id" : "20220103003", "order_time" : ISODate("2022-01-03T11:00:00Z"), "status" : "paid", "amount" : 600 }
{ "_id" : ObjectId("61fa674a511db968ca810dae"), "order_id" : "20220103004", "order_time" : ISODate("2022-01-03T12:00:00Z"), "status" : "paid", "amount" : 1200 }
After running the above command, all order records with an amount less than or equal to 1000 yuan, i.e. the first and third records, will be returned:
{ "_id" : ObjectId("61fa674a511db968ca810dab"), "order_id" : "20220103001", "order_time" : ISODate("2022-01-03T09:00:00Z"), "status" : "paid", "amount" : 800 }
{ "_id" : ObjectId("61fa674a511db968ca810dad"), "order_id" : "20220103003", "order_time" : ISODate("2022-01-03T11:00:00Z"), "status" : "paid", "amount" : 600 }
Conclusion
The $lte
operator can be used to query for records in a collection where a specified field is less than or equal to a given value. When using this operator, it is important to pay attention to the syntax format and ensure that the data types of the field name and value are correct.