On this page本页内容
$lte
Syntax: 语法:{ field: { $lte: value } }
$lte
selects the documents where the value of the 选择field
is less than or equal to (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 } } ] )
Consider the following example:考虑以下示例:
db.inventory.find( { quantity: { $lte: 20 } } )
This query will select all documents in the 此查询将选择inventory
collection where the quantity
field value is less than or equal to 20
.inventory
集合中quantity
字段值小于或等于20
的所有单据。
Example output:示例输出:
{ _id: ObjectId("61ba453ffe687fce2f04241c"), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } }
The following example sets the 以下示例基于price
field based on a $lte
comparison against a field in an embedded document.$lte
与嵌入文档中的字段的比较来设置price
字段。
db.inventory.updateMany( { "carrier.fee": { $lte: 5 } }, { $set: { price: 9.99 } } )
Example output:示例输出:
{ _id: ObjectId("61ba453ffe687fce2f04241a"), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, price: 9.99 }, { _id: ObjectId("61ba453ffe687fce2f04241b"), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 }, price: 9.99 }, { _id: ObjectId("61ba453ffe687fce2f04241c"), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 }, price: 9.99 }
This 此updateMany()
operation searches for an embedded document, carrier
, with a subfield named fee
. updateMany()
操作搜索嵌入的文档carrier
,其子字段名为fee
。It sets 它在每个{ price: 9.99 }
in each document where fee
has a value less than or equal to 5.fee
值小于或等于5的文档中设置{price:9.99}
。
To set the value of the 要仅在price
field in only the first document where carrier.fee
is less than or equal to 5, use updateOne()
.carrier.fee
小于或等于5的第一个文档中设置price
字段的值,请使用updateOne()
。