$lte Operator - ( MongoDB Misc )
In MongoDB, the $lte
operator is used to select the documents where the value of a given field is less than or equal to a specified value. This operator is commonly used in combination with other query operators to retrieve a subset of the documents in a collection.
Syntax
The basic syntax of $lte
operator in MongoDB is as follows:
{ field: { $lte: value } }
Where:
field
: the field to which the operator is applied.$lte
: the operator to be used, (in this case, it's "less than or equal to").value
: the value that the field should be less than or equal to.
Example
Consider the following collection named sales
:
{
"_id" : 1,
"product" : "T-shirt",
"quantity" : 10,
"price" : 7.5
},
{
"_id" : 2,
"product" : "Jeans",
"quantity" : 5,
"price" : 25.0
},
{
"_id" : 3,
"product" : "Sneakers",
"quantity" : 15,
"price" : 40.0
},
{
"_id" : 4,
"product" : "Hoodie",
"quantity" : 2,
"price" : 30.0
}
To retrieve the documents where the quantity is less than or equal to 10, you can use the following query:
db.sales.find({ quantity: {$lte: 10} })
Output
The above query will return the following result:
{
"_id" : 1,
"product" : "T-shirt",
"quantity" : 10,
"price" : 7.5
},
{
"_id" : 2,
"product" : "Jeans",
"quantity" : 5,
"price" : 25.0
},
{
"_id" : 4,
"product" : "Hoodie",
"quantity" : 2,
"price" : 30.0
}
Explanation
The $lte
operator selects the documents where the value of the quantity
field is less than or equal to 10. In this example, the query returns all the documents with _id
1, 2 and 4 because they both satisfy the condition.
Use
The $lte
operator is used in MongoDB queries to filter documents based on their values. It is useful in scenarios where you need to retrieve documents that have a certain value that is less than or equal to a given value.
Important Points
$lte
operator is used to select documents where the value of a field is less than or equal to a specified value.- It is used in conjunction with other query operators to retrieve a subset of the documents in a collection.
$lte
operator is useful in various scenarios where you need to filter documents based on their values in MongoDB.
Summary
In this page, we discussed the $lte
operator in MongoDB. We covered the syntax, examples, output, explanation, use cases, important points, and summary of the $lte
operator. With the knowledge of this operator, you can apply it in your MongoDB queries to filter documents based on their values that are less than or equal to a given value.