$gt

$gt

Syntax: {field: {$gt: value} }

$gt selects those documents where the value of the field is greater than (i.e. >) the specified value.选择field值大于(即>)指定值的文档。

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比较。

Consider the following example:考虑下面的例子:

db.inventory.find( { qty: { $gt: 20 } } )

This query will select all documents in the inventory collection where the qty field value is greater than 20.此查询将选择inventory集合中qty字段值大于20的所有单据。

Consider the following example that uses the $gt operator with a field from an embedded document:考虑下面的示例,该示例使用$gt运算符和来自嵌入文档的字段:

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

This update() operation will set the value of the price field in the first document found containing the embedded document carrier whose fee field value is greater than 2.update()操作将在找到的第一个包含fee字段值大于2的嵌入式文档carrier的文档中设置price字段的值。

To set the value of the price field in all documents containing the embedded document carrier whose fee field value is greater than 2, specify the multi:true option in the update() method:要在包含fee字段值大于2的嵌入式文档carrier的所有文档中设置price字段的值,请在update()方法中指定multi:true选项:

db.inventory.update(
   { "carrier.fee": { $gt: 2 } },
   { $set: { price: 9.99 } },
   { multi: true }
)

See also参阅

find(), update(), $set.