| by Arround The Web | No comments

MongoDB Less Than or Equal To

“In MongoDB, we can use the built-in comparison operators to perform various comparison operations. We can use these operators to compare values in a given document or a native value.

This post will focus on the less than or equal to operator. This operator allows us to select documents where the values in a given field are less than or equal to a given value.”

Let us explore.

Operator Syntax

The following shows the operator syntax and how to use it.

{ field: { $lte: value } }

 
The query should return the results where the values of a given field are less than or equal to the value specified in the operator.

Let us explore some basic examples.

Practical Example

Consider the example collection and documents provided in the query below:

db.createCollection("shop")
db.shop.insertMany([
    { _id: 0, "product_name": "Apple MacBook Air", "quantity": 100, "price": 1199 },
    { _id: 1, "product_name": "Google Pixel 6A", "quantity": 1000, "price": 449 },
    { _id: 2, "product_name": "Apple iPad Air", "quantity": 550, "price": 599 },
    { _id: 3, "product_name": "Samsung Z Fold 4", "quantity": 400, "price": 1799 },
    { _id: 4, "product_name": "Sony WH-100XM5", "quantity": 800, "price": 400 },
])

 
Once we have the target documents, we can proceed and use the $lte operator.

Fetch Documents Matching a Specific Condition

We can use the less than or equal to operator to fetch documents matching a specific condition.

For example, we can fetch documents whose price is less than or equal to 600. The query is as shown:

db.shop.find({price: {$lte: 600}})

 
This should filter and return the requested documents as shown:

{
    "_id" : 1.0,
    "product_name" : "Google Pixel 6A",
    "quantity" : 1000.0,
    "price" : 449.0
}
{
    "_id" : 2.0,
    "product_name" : "Apple iPad Air",
    "quantity" : 550.0,
    "price" : 599.0
}
{
    "_id" : 4.0,
    "product_name" : "Sony WH-100XM5",
    "quantity" : 800.0,
    "price" : 400.0
}

 

Updating Documents Matching a Specific Condition

We can also use the less than or equal to operator to update documents matching a specific condition.

For example, suppose we wish to increase the number of products priced at 400 and below.

We can run the query as shown below:

db.shop.updateMany({price: {$lte: 400}}, {$set: {quantity: 1600}})

 
The query should fetch all the documents with a price value less than or equal to 400. It will then pass that to the set operator and update the quantity to 1600.

We can then query the collection to view the changes as:

db.shop.find({})

 
Resulting output:

-------------------OUTPUT TRUNCATED------------------
{
    "_id" : 3.0,
    "product_name" : "Samsung Z Fold 4",
    "quantity" : 400.0,
    "price" : 1799.0
}
{
    "_id" : 4.0,
    "product_name" : "Sony WH-100XM5",
    "quantity" : 1600.0,
    "price" : 400.0
}

 
You can use the updateOne() method to update a single document matching the specified document.

Ending

In this post, we explored how to use MongoDB less than or equal to operators to fetch documents matching a given set of conditions. We hope you enjoyed this tutorial. Check out our other MongoDB tutorials to learn more.

Share Button

Source: linuxhint.com

Leave a Reply