Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Find MongoDB records with Price less than a specific value
To find MongoDB records with Price less than a specific value, use the $lt (less than) comparison operator. This operator filters documents where the specified field value is less than the given value.
Syntax
db.collection.find({
"field": { $lt: value }
});
Sample Data
Let us create a collection with sample price documents ?
db.demo728.insertMany([
{Price: 75},
{Price: 59},
{Price: 79},
{Price: 89}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5eab413c43417811278f589b"),
ObjectId("5eab414043417811278f589c"),
ObjectId("5eab414543417811278f589d"),
ObjectId("5eab414843417811278f589e")
]
}
Display all documents from the collection ?
db.demo728.find();
{ "_id": ObjectId("5eab413c43417811278f589b"), "Price": 75 }
{ "_id": ObjectId("5eab414043417811278f589c"), "Price": 59 }
{ "_id": ObjectId("5eab414543417811278f589d"), "Price": 79 }
{ "_id": ObjectId("5eab414843417811278f589e"), "Price": 89 }
Example: Find Records with Price Less Than 80
db.demo728.find({Price: {$lt: 80}});
{ "_id": ObjectId("5eab413c43417811278f589b"), "Price": 75 }
{ "_id": ObjectId("5eab414043417811278f589c"), "Price": 59 }
{ "_id": ObjectId("5eab414543417811278f589d"), "Price": 79 }
Conclusion
The $lt operator effectively filters documents based on numeric comparisons. It returns all documents where the specified field value is strictly less than the provided threshold value.
Advertisements
