MongoDB $gt Operator Examples
Let’s look at some examples of the $gt operator in MongoDB to understand it better.
In the following examples, we are working with:
Database: w3wiki
Collection: employee
Document: four documents that contain the details of the employees in the form of field-value pairs.
Example 1
In this example, we are selecting those documents where the value of the salary field is greater than 35000.
Query:
db.employee.find({salary: {$gt: 35000}}).pretty()
Output:
Example 2
In this example, we are selecting only those documents where the age of the employee is greater than 23. Or in other words, in this example, we are specifying conditions on the field in the embedded document using dot notation.
Query:
db.employee.find({"personalDetails.age": {$gt:23}}).pretty()
Output:
Using $gt operator with arrays
In this example, we are selecting only those documents where the points array is greater than the specified array.
Query:
db.employee.find({points: {$gt: [4,5]}}).pretty()
Output:
MongoDB $gt Operator
MongoDB $gt operator selects documents where the value of a field is greater than a specified value.
Contact Us