定义
5.0 版本中的更改。
兼容性
可以使用 $expr 查找托管在以下环境中的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
{ $expr: { <expression> } }
参数可以是任何有效的表达式。
行为
$lookup 操作中的 $expr
当 $expr 出现在属于 $lookup 子管道的 $match 阶段时, $expr 可以引用 $lookup 阶段定义的 let 变量。有关示例,请参见使用多个连接条件和关联子查询。
放置在 $expr 操作符上的 $eq、$lt、$lte、$gt 和 $gte 比较操作符可以使用 $lookup 阶段引用的 from 集合上的索引。限制:
索引只能用于字段和常量之间的比较,因此
let操作数必须解析为常量。示例,
$a和常量值之间的比较可以使用索引,但$a和$b之间的比较不能使用索引。当
let操作数解析为空值或缺失值时,索引不用于比较。不使用多键索引。
示例
比较同一文档中的两个字段
$expr 可以包含对同一文档中的字段进行比较的表达式。
创建一个包含这些文档的 monthlyBudget 集合:
db.monthlyBudget.insertMany( [ { _id : 1, category : "food", budget : 400, spent : 450 }, { _id : 2, category : "drinks", budget : 100, spent : 150 }, { _id : 3, category : "clothes", budget : 100, spent : 50 }, { _id : 4, category : "misc", budget : 500, spent : 300 }, { _id : 5, category : "travel", budget : 200, spent : 650 } ] )
以下操作使用 $expr 来查找 spent 金额超过 budget 的文档:
db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } )
输出:
{ _id : 1, category : "food", budget : 400, spent : 450 } { _id : 2, category : "drinks", budget : 100, spent : 150 } { _id : 5, category : "travel", budget : 200, spent : 650 }
将 $expr 与条件语句结合使用
某些查询需要在定义查询过滤器时执行条件逻辑的能力。聚合管道提供 $cond 操作符来表达条件语句。通过将 $expr 与 $cond 操作符一起使用,您可以为查询语句指定条件过滤器。
使用以下文档创建示例 supplies 集合:
db.supplies.insertMany( [ { _id : 1, item : "binder", qty : Int32("100"), price : Decimal128("12") }, { _id : 2, item : "notebook", qty : Int32("200"), price : Decimal128("8") }, { _id : 3, item : "pencil", qty : Int32("50"), price : Decimal128("6") }, { _id : 4, item : "eraser", qty : Int32("150"), price : Decimal128("3") }, { _id : 5, item : "legal pad", qty : Int32("42"), price : Decimal128("10") } ] )
假设在下个月即将举行的促销活动中,您想提供价格折扣:
如果
qty大于或等于 100,折扣价格将为price的 0.5。如果
qty小于 100,折扣价就是price的 0.75。
在应用折扣之前,您想知道 supplies 集合中哪些商品的折扣价格低于 5。
以下示例使用 $expr 和 $cond 根据 qty 和 $lt 来计算折扣价格,以返回计算出的折扣价格小于 Decimal128("5") 的文档:
// Aggregation expression to calculate discounted price let discountedPrice = { $cond: { if: { $gte: ["$qty", 100] }, then: { $multiply: ["$price", Decimal128("0.50")] }, else: { $multiply: ["$price", Decimal128("0.75")] } } }; // Query the supplies collection using the aggregation expression db.supplies.find( { $expr: { $lt:[ discountedPrice, Decimal128("5") ] } });
下表显示了每个文档的折扣价格以及折扣价格是否小于 Decimal128("5")(即文档是否满足查询条件)。
文档 | 折扣价格 | < Decimal128("5") |
|---|---|---|
{"_id": 1, "item": "binder", "qty": 100, "price": Decimal128("12") } | Decimal128("6.00") |
|
{"_id": 2, "item": "notebook", "qty": 200, "price": Decimal128("8") } | Decimal128("4.00") |
|
{"_id": 3, "item": "pencil", "qty": 50, "price": Decimal128("6") } | Decimal128("4.50") |
|
{"_id": 4, "item": "eraser", "qty": 150, "price": Decimal128("3") } | Decimal128("1.50") |
|
{"_id": 5, "item": "legal pad", "qty": 42, "price": Decimal128("10") } | Decimal128("7.50") |
|
db.collection.find() 操作返回计算出的折扣价低于 Decimal128("5") 的文档:
{ _id : 2, item : "notebook", qty : 200 , price : Decimal128("8") } { _id : 3, item : "pencil", qty : 50 , price : Decimal128("6") } { _id : 4, item : "eraser", qty : 150 , price : Decimal128("3") }
尽管 $cond 计算出了有效的折扣价格,但该价格并没有反映在返回的文档中。相反,返回的文档展示了原始状态下的匹配文档。查找操作没有返回 binder 或 legal pad 文档,因为其折扣价格大于 5。