$lt
The $lt aggregation operator compares two values and returns true if the first value is less than 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 $lt operator to identify low stock items.
Create sample documents
db.warehouse.insertMany([ { _id: 1, item: "Bolts", stock: 5 }, { _id: 2, item: "Nuts", stock: 25 }, { _id: 3, item: "Screws", stock: 8 } ]);
Query example
db.warehouse.aggregate([ { $project: { item: 1, stock: 1, lowStock: { $lt: ["$stock", 10] } } } ]);
Output
[
{ _id: 1, item: 'Bolts', stock: 5, lowStock: true },
{ _id: 2, item: 'Nuts', stock: 25, lowStock: false },
{ _id: 3, item: 'Screws', stock: 8, lowStock: true }
]
Code examples
To view a code example for using the $lt aggregation operator, choose the tab for the language that you want to use: