$lt
On this page本页内容
Definition定义
$lt
-
Syntax:
{ field: { $lt: value } }
$lt
selects the documents where the value of the选择field
is less than (i.e.<
) the specifiedvalue
.field
值小于(即<
)指定值的文档。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.对于大多数数据类型,比较运算符只对BSON类型与查询值类型匹配的字段执行比较。MongoDB通过类型支架支持有限的跨BSON比较。
Examples实例
The following examples use the 以下示例使用inventory
collection. Create the collection:inventory
集合。创建集合:
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 less than 20
:inventory
集合中quantity
小于20
的所有单据:
db.inventory.find( { quantity: { $lt: 20 } } )
Example output:示例输出:
{
_id: ObjectId("61ba634dfe687fce2f04241f"),
item: 'washers',
quantity: 10,
carrier: { name: 'Shipit', fee: 1 }
}
Perform an Update Based on Embedded Document Fields基于嵌入的文档字段执行更新
The following example sets the 以下示例根据price
field based on a $lt
comparison against a field in an embedded document.$lt
与嵌入文档中某个字段的比较设置price
字段。
db.inventory.updateMany( { "carrier.fee": { $lt: 20 } }, { $set: { price: 9.99 } } )
Example output:输出示例:
{
_id: ObjectId("61ba634dfe687fce2f04241d"),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 },
price: 9.99
},
{
_id: ObjectId("61ba634dfe687fce2f04241e"),
item: 'bolts',
quantity: 50,
carrier: { name: 'Shipit', fee: 4 },
price: 9.99
},
{
_id: ObjectId("61ba634dfe687fce2f04241f"),
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
. It sets { price: 9.99 }
in each document where fee
has a value less than 20.updateMany()
操作搜索带有一个名为fee
的子字段的嵌入文档carrier
。它在每个费用值小于20
的文档中设置{ price: 9.99 }
。
To set the value of the 要仅在price
field in only the first document where carrier.fee
is less than 20, use updateOne()
.carrier.fee
小于20
的第一个文档中设置价格字段的值,请使用updateOne()
。