$lte
The $lte aggregation operator compares two values and returns true if the first value is less than or equal to the second, otherwise returns false.
Parameters
-
expression1: The first value to compare. -
expression2: The second value to compare.
Example (MongoDB Shell)
The following example demonstrates using the $lte operator to identify budget-friendly items.
Create sample documents
db.menu.insertMany([ { _id: 1, dish: "Salad", price: 8 }, { _id: 2, dish: "Pasta", price: 12 }, { _id: 3, dish: "Soup", price: 6 } ]);
Query example
db.menu.aggregate([ { $project: { dish: 1, price: 1, affordable: { $lte: ["$price", 10] } } } ]);
Output
[
{ _id: 1, dish: 'Salad', price: 8, affordable: true },
{ _id: 2, dish: 'Pasta', price: 12, affordable: false },
{ _id: 3, dish: 'Soup', price: 6, affordable: true }
]
Code examples
To view a code example for using the $lte aggregation operator, choose the tab for the language that you want to use: