Visit complete MongoDB roadmap

← Back to Topics List

$gt

The $gt operator in MongoDB is used to filter documents based on the values of a particular field being greater than the specified value. This operator is handy when you want to retrieve documents that fulfill a condition where a field’s value is more than a given value.

The general syntax for querying using the $gt operator is:

{
  field: {
    $gt: value;
  }
}

Here, we need to replace the field with the actual field name in the document, and value with the desired value you want to compare against.

Example

Consider a students collection where each document contains information about a student, including their first_name, last_name, and age.

If you want to find all the students whose ages are greater than 21, you would use a query with the $gt operator as follows:

db.students.find({ age: { $gt: 21 } });

This query will return all the documents in the students collection where the age field has a value greater than 21.

Keep in mind that the $gt operator can also be used with non-numeric data types, such as date values. The comparison will be made based on their natural order.

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.