$expr¶
On this page
Definition¶
New in version 3.6.
-
$expr¶ Allows the use of aggregation expressions within the query language.
$exprhas the following syntax:The arguments can be any valid expression. For more information on expressions, see Expressions.
Behavior¶
$exprcan build query expressions that compare fields from the same document in a$matchstage.$exprdoes not support multikey indexes.
Examples¶
Compare Two Fields from A Single Document¶
Consider an monthlyBudget collection with the following documents:
The following operation uses $expr to find documents
where the spent amount exceeds the budget:
The operation returns the following results:
Using $expr With Conditional Statements¶
Some queries require the ability to execute conditional logic when
defining a query filter. The aggregation framework provides the
cond operator for applying conditional logic to a
pipeline stage. You can use $expr with the $cond
operator to query based on conditional modifications to the source data.
Consider a supplies collection with the following documents. Each
document in the supplies collection represents an item, its
quantity, and its price:
An application automatically applies a 50% discount to the price of
any item with a quantity of more than 100. Otherwise, the
application applies a 25% discount to the price.
A user needs to know which items in the supplies collection have a
discounted price of less than 5. The user writes a query that:
- Uses conditional logic to identify the discount applicable to a given document.
- Calculates that document’s discounted price.
- Only returns documents whose discounted price is less than
5.
The query returns only documents from the supplies collection whose
discounted price is less than 5.
The following operation implements the previous steps using the
$expr operator to access the cond and
$lt aggregation operators:
$condmodifies the input document depending on the value ofqty. Specifically, it compares theqtyusing$gteand modifies the price using$multiply.- The document
_id : 1has aqtyof 100.$condreturns the result of multiplying thepriceby0.50, effectively applying a50%discount, yielding a value of6. - The document
id : 3has aqtyof50.$condreturns the result of multiplying thepriceby0.75, effectively applying a25%discount, yielding a value of4.
- The document
$ltcompares the returned value to the provided value5.- The document
_id : 1returns6fromcond. Since$lt [6, 5]evaluates to false, the expression does not return the document. - The document
_id : 3returns4fromcond. Since$lt [4, 5]evaluates to true, the expression returns the document.
- The document
find() continues to apply $cond
to each document in the supplies collection. The operation returns
the following results:
$cond modifies the input documents as part of the query
filtering but does not persist the modifications to disk. The results
represent the matching documents in their original state. The find
operation did not return the binder or legal pad documents, as
their modified price was greater than 5. Specifically:
binderhas a quantity of200. The$condlogic applies a.75multiplier which resolves to6. Since6is greater than5,find()filters out thebinderdocument.legal padhas a quantity of42. The$condlogic applies a.50multiplier which resolves to5. Since the operation used the$ltoperator, the modified price is not less than5andfind()filters out thelegal paddocument.