$gte

On this page本页内容

Definition定义

$gte

Syntax: 语法:{ field: { $gte: value } }

$gte selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value (e.g. 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比较。

Examples示例

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 }
   }
] )

Match Document Fields匹配文档字段

Select all documents in the inventory collection where quantity is greater than or equal to 20:选择inventory集合中quantity大于或等于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 }
}

Perform an Update Based on Embedded Document Fields基于嵌入式文档字段执行更新

The following example sets the price field based on a $gte comparison against a field in an embedded document.以下示例基于$gte与嵌入文档中的字段的比较设置price字段。

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
}

This updateOne() operation searches for an embedded document, carrier, with a subfield named fee. updateOne()操作使用名为fee的子字段搜索嵌入文档carrierIt sets { price: 9.99 } in each document where fee has a value greater than or equal to 2.它在每个fee值大于或等于2的文档中设置{price:9.99}

To set the value of the price field in only the first document where carrier.fee is greater than 2, use updateOne().要仅在carrier.fee大于2的第一个文档中设置价格字段的值,请使用updateOne()

Tip提示
See also: 参阅:
←  $gt$in →