The $literal operator returns a constant value in an aggregation expression. MongoDB does not treat it as a field path, which matters when your value starts with $ or when you need a fixed array or object inside another operator.
01
Constant Values
Fixed strings and numbers.
02
Syntax
{ $literal: value }.
03
Field vs Literal
When $ matters.
04
Arrays & Objects
Literal collections.
05
Use Cases
Labels, defaults, tags.
06
With Operators
$cond, $concatArrays.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $literal operator evaluates to the value you provide — exactly as written. Unlike a plain string in an expression, $literal prevents MongoDB from interpreting $-prefixed strings as field references.
For example, "$price" in $project normally reads the price field from the document. Wrapping it as { $literal: "$price" } outputs the string "$price" instead.
💡
Beginner Tip
Plain constants like "USD" or 42 often work without $literal in simple $project fields. Reach for $literal when the value could be parsed as a field path, or when an operator expects an expression object rather than a raw constant.
Foundation
📝 Syntax
The $literal operator wraps a single constant value:
// Notebook:
// fieldRef: 12.5 ← reads the price field
// literalString: "$price" ← outputs the string "$price"
How It Works
Without $literal, "$price" is a field path. With $literal, MongoDB returns the exact string — useful for documentation fields, template placeholders, or debugging output.
Example 3 — $literal Inside $cond
Assign pass/fail labels based on a price threshold:
$literal can wrap objects and arrays. Every document receives the same meta structure without reading from fields.
Applications
🚀 Use Cases
Fixed labels — add currency codes, status strings, or category names to output.
$-prefixed strings — output template tokens like "$price" without field lookup.
Default values in expressions — supply constants inside $cond, $switch, or $ifNull branches.
Literal arrays/objects — pass fixed collections to $concatArrays, $setUnion, or $in.
🧠 How $literal Works
1
You wrap a value
Provide the constant inside { $literal: ... } in an aggregation expression.
Input
2
MongoDB skips field parsing
Unlike "$price", the wrapped value is not treated as a document field path.
Parse
3
The constant is returned
The expression evaluates to your value — string, number, array, or object — for each document.
Output
=
📊
Predictable constants
Your pipeline outputs exactly the fixed value you intended, with no accidental field lookups.
Wrap Up
Conclusion
The $literal operator is the safe way to include constant values in aggregation expressions. Use it when a value might be mistaken for a field path, or when another operator expects an expression object rather than a bare constant.
For simple $project fields, plain strings and numbers often suffice. Inside nested operators like $cond and $concatArrays, $literal makes your intent explicit and prevents subtle parsing mistakes.
Wrap arrays/objects when passing them into expression operators
Use $literal in $cond branches for fixed strings
Prefer explicit literals in complex nested expressions
Test output when mixing field paths and constants
❌ Don’t
Assume "$field" is a string — it is a field path
Use $literal as a pipeline stage
Wrap field references you actually want to read
Overuse $literal for simple top-level $project strings
Confuse $literal with $let variables ($$name)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $literal
Use these points when adding constants to aggregation expressions.
5
Core concepts
📝01
Constant Value
Returns as-is.
Purpose
🔢02
$ Strings
No field parsing.
Syntax
🛠03
Any BSON Type
String to object.
Types
🔄04
In $cond
Fixed branch labels.
Pattern
📑05
With Arrays
$concatArrays etc.
Advanced
❓ Frequently Asked Questions
$literal returns a constant value in an aggregation expression without treating strings that start with $ as field paths. Use it when you need a fixed string, number, array, or object in $project, $addFields, or nested expressions.
The syntax is { $literal: <value> }. The value can be a string, number, boolean, null, array, or object. MongoDB returns it exactly as written, without field-path parsing.
Use $literal when a value would be misread as a field reference. For example, "$price" normally reads the price field, but { $literal: "$price" } outputs the string "$price". It is also useful for literal arrays inside operators like $concatArrays.
Yes. $literal supports strings, numbers, booleans, null, arrays, and objects. MongoDB returns the wrapped value as a constant in the expression result.
Use $literal inside expression stages such as $project, $addFields, $set, and within other operators like $cond, $concatArrays, $in, and $setEquals when you need fixed constant values.