$literal is a MongoDB aggregation expression that returns a constant value as written. MongoDB will not treat it as a field path or operator—even if the value is a string that starts with $.
Use it when the value starts with $, or when you want an obvious fixed constant.
Skip $literal for plain numbers and normal strings that have no $ ambiguity.
Piece
Required?
Role
<value>
Yes
Constant to return unchanged
String with $
Common case
Stops field-path parsing
Number / bool / null
Optional wrap
Makes “constant” intent explicit
Pseudocode
Pseudocode
for each document:
// without $literal
"$price" → look up document.price
// with $literal
{ $literal: "$price" } → the string "$price"
{ $literal: "active" } → the string "active"
emit { ..., field: that constant }
Reference
Cheat Sheet
Keep these facts handy when embedding constants in pipelines.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $literal: <value> }
Main job
Return a constant; skip field-path parsing
Classic trap
"$price" vs { $literal: "$price" }
Accepts
string, number, bool, null, array, object
Not needed for
Most plain numbers / non-$ strings
Sample
{ $literal: "active" } → "active"
Try it
Live Preview
Compare a field path with $literal on the same Notebook document ({ price: 50 }.
Simulates field paths vs { $literal: … } on { price: 50 }.
Result
document = { price: 50 }
expression = "$price"
→ reads field price
result → 50
Trace
Worked Walkthrough — Field vs Literal
A Notebook costs 50. Writing "$price" reads that field. Wrapping the same characters in $literal keeps them as text.
Calculation
Document:
{ item: "Notebook", price: 50 }
"$price"
→ look up price
→ 50
{ $literal: "$price" }
→ do not look up
→ string "$price"
{ $literal: "active" }
→ constant label
→ "active"
Code
MongoDB Examples
We use one product document. First store the sample, then add a constant status, then show how $literal protects a $ string. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one product: a Notebook priced at 50. We save it in a collection named products.
One line:$literal answers “keep this value exactly as written—do not treat it as a field.”
Frequently Asked Questions
$literal returns a constant value in an aggregation expression. MongoDB does not treat the value as a field path or operator—even if it is a string that starts with $.
Use { $literal: <value> }. The value can be a string, number, boolean, null, array, or object. MongoDB returns it exactly as written.
Use it 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". Plain numbers and most strings do not need $literal.
Yes. Strings, numbers, booleans, null, arrays, and objects all work. MongoDB returns the wrapped value as a constant.
Anywhere an aggregation expression is allowed—$addFields, $project, $set, and inside other operators when you need a fixed constant.
No. Use it when ambiguity exists (especially strings starting with $) or when an operator argument must be a fixed value that could otherwise look like an expression.