$gt
The $gt aggregation operator compares two values and returns true if the first value is greater 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 $gt operator to identify products exceeding a price threshold.
Create sample documents
db.products.insertMany([ { _id: 1, name: "Laptop", price: 1200 }, { _id: 2, name: "Mouse", price: 25 }, { _id: 3, name: "Monitor", price: 400 } ]);
Query example
db.products.aggregate([ { $project: { name: 1, price: 1, expensive: { $gt: ["$price", 100] } } } ]);
Output
[
{ _id: 1, name: 'Laptop', price: 1200, expensive: true },
{ _id: 2, name: 'Mouse', price: 25, expensive: false },
{ _id: 3, name: 'Monitor', price: 400, expensive: true }
]
Code examples
To view a code example for using the $gt aggregation operator, choose the tab for the language that you want to use: