$gt¶
On this page
Definition¶
-
$gt
¶ Syntax:
{ field: { $gt: value } }
$gt
selects those documents where the value of thefield
is greater than (i.e.>
) the specifiedvalue
.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.
Examples¶
The following examples use the inventory
collection. Create the
collection:
Match Document Fields¶
Select all documents in the inventory
collection where quantity
is greater than 20
:
Example output:
Perform an Update Based on Embedded Document Fields¶
The following example sets the price
field based on a $gt
comparison against a field in an embedded document.
Example output:
This updateOne()
operation searches for an
embedded document, carrier
, with a subfield named fee
. It sets
{ price: 9.99 }
in the first document it finds where fee
has a
value greater than 2.
To set the value of the price
field in all documents where
carrier.fee
is greater than 2, use
updateMany()
.