Updated December 13, 2023
MongoDB $lt Operator
MongoDB has various operators for comparison. $lt is a MongoDB operator where you can select a value less than (<), as in the docs. You can use $lt in methods like find(), update(), etc.
Several comparison operators can perform comparisons on fields where the BSON type matches the value of the query. MongoDB provides only limited support for cross-BSON comparison via backing types.
Table of Contents
- MongoDB $lt operator
- Compatibility
- Syntax
- Update based on Embedded Document Fields
- Examples using the $lt operator in MongoDB
Compatibility
The $lt is used for hosted deployments in these MongoDB environments:
- Atlas: Cloud-Managed MongoDB
- Enterprise: paid, self-managed MongoDB
- Community: Free, self-managed MongoDB
Syntax
The syntax of this $lt operator is as follows:
{field: { $lt: value } }
Consider this example, where we will use the $lt operator.
We will create an “educba” db in Mongodb in the shell:
Now, we will create an “inventory” collection under the “educba” db:
Now, we will insert these values in the “inventory” collection:
Code:
db.inventory.insertMany( [
{
"item": "screws", "quantity": 20,
"carrier": { "name": "Shipit", "fee": 2 }
},
{
"item": "nails", "quantity": 40,
"carrier": { "name": "Shipit", "fee": 5 }
},
{
"item": "rivets", "quantity": 15,
"carrier": { "name": "Shipit", "fee": 2.5 }
},
{
"item": "nuts", "quantity": 30,
"carrier": { "name": "Shipit", "fee": 3 }
},
{
"item": "bolts", "quantity": 50,
"carrier": { "name": "Shipit", "fee": 4 }
},
{
"item": "washers", "quantity": 10,
"carrier": { "name": "Shipit", "fee": 1 }
}
] )
Output:
These will be inserted successfully.
We have added this to collections of MongoDB databases. Now, we will query the following:
dD.binventory.find({ "quantity": { "$lt": 30 } })
This query finds all values which have a quantity less than 30. There will be three outputs:
Update based on Embedded Document Fields
You can update based on embedded document fields using the $lt operator.
For example, if you want to increase fees for carriers whose volume is less than 4. So you can use the following command:
Code:
db.inventory.updateMany(
{ "carrier.fee": { "$lt": 4 } },
{ "$inc": { "carrier.fee": 2 } }
)
This query uses the $lt operator to find documents where the charge inside the carrier-embedded document is less than 4. First, we filter {“carrier.fee”: { “$lt”: 4 } }, then it will update all those values. Then, the $inc operator increases the charge to 2. The final documents are after the above update query:
Examples using $lt Operator in MongoDB
Consider another collection to understand the $lt operator in the given examples.
Using the MongoDB shell, we will create “devices” db:
We will create a collection of “devices” under the “devices” db:
Now, we will insert these values into the “devices” collection:
Code:
db.devices.insertMany([
{ "_id": 1, "deviceName": "UltraBook," "cost": 999, "manufactureDate": ISODate("2018-03-22"), "specs": { "ram": 8, "display": 13.3, "processor": 2.5 }, "colors": ["silver", "black"], "storage": [128, 256, 512] },
{ "_id": 2, "deviceName": "TabMax," "cost": 1199, "manufactureDate": ISODate("2019-07-10"), "specs": { "ram": 12, "display": 10.5, "processor": 3.2 }, "colors": ["white," "black," "blue"], "storage": [256, 512, 1024] },
{ "_id": 3, "deviceName": "SmartPad Pro," "cost": 899, "manufactureDate": ISODate("2020-12-05"), "specs": { "ram": 6, "display": 9.7, "processor": 2.8 }, "colors": ["gray," "rose gold"], "storage": [64, 128, 256] },
{ "_id": 4, "deviceName": "SmartDesk," "cost": 1499, "manufactureDate": ISODate("2022-05-18"), "specs": { "ram": 16, "display": 27, "processor": 4.0 }, "colors": ["black," "white"], "storage": [512, 1024, 2048] },
{ "_id": 5, "deviceName": "SmartPhone Pro," "cost": 1299, "manufactureDate": ISODate("2023-10-08"), "specs": { "ram": 8, "display": 6.2, "processor": 3.0 }, "colors": ["gold," "blue," "green"], "storage": [256, 512] }
]);
It will be inserted successfully:
This collection has fields: deviceName, cost, manufactureDate, specs, colors, and storage. These documents are used in examples with the $lt operator for the following various queries.
Example 1
Select documents where the cost is less than 1199:
Code:
db.devices.find({
cost: {
$lt: 1199
}
}, {
deviceName: 1,
cost: 1
})
The output will be,
Example 2
Check if the display size in the specs document is less than 10:
Code:
db.devices.find({
"specs.display": {
$lt: 10
}
}, {
deviceName: 1,
"specs.display": 1
})
The output will be,
Example 3
Check if an array element in storage is less than 1024:
Code:
db.devices.find({
storage: {
$lt: 1024
}
}, {
deviceName: 1,
storage: 1
})
The output will be,
Example 4
Select documents where the manufacture date is before 2022-01-01:
Code:
db.devices.find({
manufactureDate: {
$lt: new ISODate('2022-01-01')
}
}, {
deviceName: 1,
manufactureDate: 1
});
The output will be,
Conclusion
The $lt is an operator in MongoDB that selects documents with a value less than (<) the given value. You can find and update values using the $lt operator. MongoDB’s $lt operator efficiently selects and updates documents based on specified criteria.
FAQ’s
Q1. How does the $lt operator work in conjunction with the find() method?
Answer: In the find() method, the $lt operator filters documents where the specified field is less than a given value. For example, db.collection.find({ field: { $lt: value } }) retrieves documents where the field value is less than the given value.
Q2. Can I use the $lt operator to update documents based on embedded document fields?
Answer: Yes. You can update documents based on embedded document fields using the $lt operator. For example, you can increase fees for carriers whose volume is less than a given value. It uses the $lt operator in combination with the updateMany() method.
Q3. Can I combine the $lt operator with others in a single query?
Answer: Yes. To create more complex conditions, you can combine the $lt operator with other operators in a query. It provides you the flexibility to construct queries for your specific requirements.
Q4. Can the $lt operator be used on non-numeric fields?
Answer: Yes. This $lt operator can be used for non-numeric fields as well. This operator is used for numerical comparisons. It can also be used with date fields, string fields, and other BSON types. You can filter documents based on various data types.
Recommended Articles
We hope this EDUCBA information on “MongoDB $lt” benefited you. You can view EDUCBA’s recommended articles for more information,