On this page本页内容
$gt
Syntax: 语法:{ field: { $gt: value } }
$gt
selects those documents where the value of the 选择field
is greater than (i.e. >
) the specified value
.field
值大于(即>
)指定value
的文档。
For most data types, comparison operators only perform comparisons on fields where the BSON type matches the query value's type. 对于大多数数据类型,比较运算符仅对BSON类型与查询值类型匹配的字段执行比较。MongoDB supports limited cross-BSON comparison through Type Bracketing.MongoDB通过类型括号支持有限的跨BSON比较。
The following examples use the 以下示例使用inventory
collection. inventory
集合。Create the collection:创建集合:
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 } } ] )
Select all documents in the 选择inventory
collection where quantity
is greater than 20
:inventory
集合中quantity
大于20
的所有文档:
db.inventory.find( { quantity: { $gt: 20 } } )
Example output:示例输出:
{ _id: ObjectId("61ba25cbfe687fce2f042414"), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 } }, { _id: ObjectId("61ba25cbfe687fce2f042415"), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 } }
The following example sets the 以下示例基于price
field based on a $gt
comparison against a field in an embedded document.$gt
与嵌入文档中的字段的比较设置price
字段。
db.inventory.updateOne( { "carrier.fee": { $gt: 2 } }, { $set: { "price": 9.99 } } )
Example output:示例输出:
{ _id: ObjectId("61ba3ec9fe687fce2f042417"), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, price: 9.99 }, { _id: ObjectId("61ba3ec9fe687fce2f042418"), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 } }, { _id: ObjectId("61ba3ec9fe687fce2f042419"), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } }
This 此updateOne()
operation searches for an embedded document, carrier
, with a subfield named fee
. updateOne()
操作使用名为fee的子字段搜索嵌入文档carrier
。It sets 它在找到的第一个文档中设置{ price: 9.99 }
in the first document it finds where fee
has a value greater than 2.{ price: 9.99 }
,其中fee
的值大于2。
To set the value of the 要在price
field in all documents where carrier.fee
is greater than 2, use updateMany()
.carrier.fee
大于2的所有文档中设置price
字段的值,请使用updateMany()
。