For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

$gte (query predicate operator)

$gte

$gte selects documents where the value of the specified field is greater than or equal (>=) to the specified value.

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

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

The $gte operator has the following form:

{ field: { $gte: value } }

Inequality comparison operators use type bracketing: a comparison on a given BSON type filters out values of a different type.

To create the inventory collection used in the examples, run:

db.inventory.insertMany( [ { item: "nuts", quantity: 30, carrier: { name: "Shipit", fee: 3 } }, { item: "bolts", quantity: 50, carrier: { name: "Shipit", fee: 4 } }, { item: "washers", quantity: 10, carrier: { name: "Shipit", fee: 1 } }] )

This example selects documents in the inventory collection where quantity is greater than or equal to 20:

db.inventory.find( { quantity: { $gte: 20 } } )

Example output:

{ _id: ObjectId("61bb51211b83c864e3bbe037"), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 } }, { _id: ObjectId("61bb51211b83c864e3bbe038"), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 }}

This example sets the price field based on a $gte comparison on a field in an embedded document:

db.inventory.updateMany( { "carrier.fee": { $gte: 2 } }, { $set: { "price": 9.99 } })

Example output:

{ _id: ObjectId("61bb51211b83c864e3bbe037"), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, price: 9.99},{ _id: ObjectId("61bb51211b83c864e3bbe038"), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 }, price: 9.99}

The updateMany() operation matches an embedded document named carrier, with a subfield named fee. It sets { price: 9.99 } in each document where fee is greater than or equal to 2.

To set the price field in only the first document where carrier.fee is greater than 2, use updateOne().