$gte
The $gte aggregation operator compares two values and returns true if the first value is greater 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 $gte operator to check if students passed an exam.
Create sample documents
db.students.insertMany([ { _id: 1, name: "Alice", score: 85 }, { _id: 2, name: "Bob", score: 60 }, { _id: 3, name: "Charlie", score: 72 } ]);
Query example
db.students.aggregate([ { $project: { name: 1, score: 1, passed: { $gte: ["$score", 70] } } } ]);
Output
[
{ _id: 1, name: 'Alice', score: 85, passed: true },
{ _id: 2, name: 'Bob', score: 60, passed: false },
{ _id: 3, name: 'Charlie', score: 72, passed: true }
]
Code examples
To view a code example for using the $gte aggregation operator, choose the tab for the language that you want to use: