Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs Menu
Docs Home
/ /

$gt (query predicate operator)

$gt

$gt selects documents where the value of the field is greater than (>) the specified value.

For most data types, comparison operators only perform comparisons on fields where the BSON type matches the query value's type. MongoDB supports limited cross-BSON comparison through Type Bracketing.

You can use $gt for deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

The $gt operator has this form:

{ field: { $gt: value } }

The examples on this page use data from the sample_mflix sample dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the sample dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

This example selects documents in the movies collection where runtime is greater than 1000 minutes:

db.movies.find(
{ runtime: { $gt: 1000 } },
{ _id: 0, title: 1, runtime: 1, plot: 1 }
)
[
{
plot: 'The economic and cultural growth of Colorado spanning two centuries from the mid-1700s to the late-1970s.',
runtime: 1256,
title: 'Centennial'
},
{
plot: 'A documentary on the history of the sport with major topics including Afro-American players, player/team owner relations and the resilience of the game.',
runtime: 1140,
title: 'Baseball'
}
]

This updateMany() operation matches an embedded document named imdb, with a subfield named rating. The operation sets { highestRated: true } in documents where rating is greater than 9.5.

db.movies.updateMany(
{ "imdb.rating" : { $gt: 9.5 } },
{ $set: { "highestRated": true } }
)
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

Back

$eq

On this page