On this page本页内容
New in version 3.6.版本3.6中的新功能。
$expr
¶Allows the use of aggregation expressions within the query language.允许在查询语言中使用聚合表达式。
$expr
has the following syntax:语法如下所示:
The arguments can be any valid aggregation expression. 参数可以是任何有效的聚合表达式。For more information, see Expressions.有关详细信息,请参阅表达式。
$expr
can build query expressions that compare fields from the same document in a $match
stage.$expr
可以构建查询表达式,用于在$match
阶段比较同一文档中的字段。$match
stage is part of a $lookup
stage, $expr
can compare fields using let
variables. $match
阶段是$lookup
阶段的一部分,$expr
可以使用let
变量比较字段。$lookup
指定多个联接条件。$expr
only uses indexes on the from
collection for equality matches in a $match
stage.$expr
仅使用from
集合上的索引进行$match
阶段中的相等匹配。$expr
does not support multikey indexes.$expr
不支持多键索引。Consider an 请考虑具有以下文档的monthlyBudget
collection with the following documents:monthlyBudget
集合:
The following operation uses 以下操作使用$expr
to find documents where the spent
amount exceeds the budget
:$expr
查找spent
(花费)金额超过budget
(预算)的文档:
The operation returns the following results:操作返回以下结果:
$expr
With Conditional Statements$expr
与条件语句一起使用¶Some queries require the ability to execute conditional logic when defining a query filter. 在定义查询筛选器时,有些查询需要能够执行条件逻辑。The aggregation framework provides the 聚合框架提供了$cond
operator to express conditional statements. $cond
运算符来表示条件语句。By using 通过将$expr
with the $cond
operator, you can specify a conditional filter for your query statement.$expr
与$cond
运算符一起使用,可以为查询语句指定条件筛选器。
Create a sample 使用以下文档创建示例supplies
collection with the following documents:supplies
集合:
Assume that for an upcoming sale next month, you want to discount the prices such that:假设对于下个月即将到来的销售,您希望折扣价格,以便:
qty
is greater than or equal to 100, the discounted price will be 0.5 of the price
.qty
大于或等于100,则折扣价格为价格的0.5。qty
is less than 100, the discounted price is 0.75 of the price
.qty
小于100,则折扣价格为price
的0.75。Before applying the discounts, you would like to know which items in the 在应用折扣之前,您想知道supplies
collection have a discounted price of less than 5
.supplies
集合中哪些商品的折扣价格低于5
。
The following example uses 以下示例使用$expr
with $cond
to calculate the discounted price based on the qty
and $lt
to return documents whose calculated discount price is less than NumberDecimal("5")
:$expr
和$cond
根据qty
计算折扣价格,使用$lt
返回计算折扣价格小于NumberDecimal("5")
的文档:
The following table shows the discounted price for each document and whether discounted price is less than 下表显示了每张单据的折扣价格,以及折扣价格是否小于NumberDecimal("5")
(i.e. whether the document meets the query condition).NumberDecimal("5")
(即单据是否满足查询条件)。
< NumberDecimal(“5”) | ||
---|---|---|
{“_id”: 1, “item”: “binder”, “qty”: 100, “price”: NumberDecimal(“12”) } | NumberDecimal(“6.00”) | false |
{“_id”: 2, “item”: “noteboook”, “qty”: 200, “price”: NumberDecimal(“8”) } | NumberDecimal(“4.00”) | true |
{“_id”: 3, “item”: “pencil”, “qty”: 50, “price”: NumberDecimal(“6”) } | NumberDecimal(“4.50”) | true |
{“_id”: 4, “item”: “eraser”, “qty”: 150, “price”: NumberDecimal(“3”) } | NumberDecimal(“1.50”) | true |
{“_id”: 5, “item”: “legal pad”, “qty”: 42, “price”: NumberDecimal(“10”) } | NumberDecimal(“7.50”) | false |
The db.collection.find()
operation returns the documents whose calculated discount price is less than NumberDecimal("5")
:db.collection.find()
操作返回计算出的折扣价格小于NumberDecimal("5")
的单据:
Even though 即使$cond
calculates an effective discounted price, that price is not reflected in the returned documents. $cond
计算了一个有效的折扣价格,该价格也不会反映在返回的文档中。Instead, the returned documents represent the matching documents in their original state. 相反,返回的文档表示原始状态下的匹配文档。The find operation did not return the 查找操作没有返回binder
or legal pad
documents, as their discounted price was greater than 5
.binder
或legal pad
文档,因为它们的折扣价格大于5
。