The $expr operator lets you use aggregation expressions inside find() queries and $match stages. It unlocks field-to-field comparisons, arithmetic filters, and dynamic conditions that standard query operators cannot handle alone.
01
Expression Queries
Aggregation ops in filters.
02
Syntax
{ $expr: { ... } }
03
Field vs Field
Compare two document fields.
04
$match Stage
Filter in aggregation pipelines.
05
Use Cases
Budget checks, validation.
06
$ Prefix Fields
"$field" inside expressions.
Fundamentals
Definition and Usage
In MongoDB, the $expr operator bridges query filters and aggregation expressions. Standard query operators like { salary: { $gt: 50000 } } compare a field to a literal value. With $expr, you can write { $expr: { $gt: [ "$spent", "$budget" ] } } to find documents where one field exceeds another — something regular queries cannot do directly.
💡
Beginner Tip
Inside $expr, field names need a $ prefix: "$spent", not "spent". This is aggregation expression syntax. Wrap comparison operators like $gt, $eq, or $and inside $expr.
Foundation
📝 Syntax
The $expr operator wraps any aggregation expression for use in a query filter:
$expr filters over-budget departments in $match, then a later $project stage computes how much each department overspent.
Applications
🚀 Use Cases
Field-to-field comparisons — find documents where spent > budget, actual ≠ expected, or similar cross-field checks.
Arithmetic filters — filter based on computed values like sale price below 50% of original price.
Dynamic validation — flag records that fail business rules expressed as aggregation expressions.
Complex conditions — combine $and, $or, and comparisons in a single find() or $match filter.
🧠 How $expr Works
1
MongoDB evaluates the inner expression
For each document, MongoDB runs the aggregation expression inside $expr — comparing fields, computing arithmetic, or checking conditions.
Evaluate
2
Expression returns true or false
Comparison and logical operators produce a boolean result for each document, just like in aggregation $project stages.
Result
3
Matching documents pass the filter
Documents where the expression is true continue through find() or $match. Others are excluded.
Filter
=
🔍
Dynamic query results
You get documents matching field-to-field rules that standard query operators alone cannot express.
Wrap Up
Conclusion
The $expr operator unlocks the full power of aggregation expressions inside query filters. It is the go-to tool when you need to compare fields on the same document, apply arithmetic in filters, or write dynamic conditions that standard query operators cannot handle.
For beginners, remember: wrap your aggregation expression in { $expr: ... }, use "$field" with a dollar prefix for field references, and reserve $expr for cases where regular query syntax is not enough.
Use $expr when comparing two fields on the same document
Prefix field names with $ inside the expression
Prefer standard query operators when comparing a field to a literal
Combine $and and $or for compound conditions
Place $expr filters early in pipelines for performance
❌ Don’t
Use $expr for simple field-vs-literal comparisons
Forget the $ prefix on field references inside expressions
Put $expr inside $project (it is a query operator)
Overuse $expr when standard query syntax works fine
Assume $expr supports every aggregation stage operator
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $expr
Use these points when writing dynamic MongoDB queries.
5
Core concepts
🔢01
Expression Queries
Aggregation ops in filters.
Purpose
📝02
Wrap Syntax
{ $expr: { ... } }
Syntax
🛠03
Field vs Field
Compare "$a" and "$b".
Usage
🔍04
Dynamic Filters
Budget, validation rules.
Use case
⚠05
$ Prefix
Fields need "$field".
Edge case
❓ Frequently Asked Questions
$expr lets you use aggregation expression operators inside find() queries and $match stages. It enables comparisons between fields, arithmetic in filters, and dynamic conditions that standard query operators cannot express.
The syntax is { $expr: <aggregation expression> }. Wrap any aggregation expression such as { $gt: [ "$spent", "$budget" ] } or { $eq: [ "$status", "active" ] } inside $expr.
Use $expr when you need to compare two fields on the same document (like spent > budget), perform arithmetic in a filter, or combine multiple aggregation operators in one condition.
Yes. Inside $expr, reference document fields with a $ prefix, like "$spent" or "$budget". This is the aggregation expression syntax, not the standard query field name.
Yes. $expr works inside the $match stage of an aggregation pipeline, using the same syntax as in find() queries.