MongoDB $literal Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $literal?

$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 $.

Remember
$literal = keep this value as-is

{ $literal: <value> }

"$price"              → read field price
{ $literal: "$price" } → string "$price"

{ $literal: "active" } → constant "active"
{ $literal: 0 }        → constant 0
UsePrefer
Fixed value / protect a $ string$literal
Name & reuse a computed value$let
Read a document field"$fieldName"

How to Use $literal in MongoDB

Wrap any constant you want MongoDB to leave untouched.

  1. Choose an expression stage ($addFields or $project).
  2. Pass the constant to { $literal: <value> }.
  3. Use it when the value starts with $, or when you want an obvious fixed constant.
  4. Skip $literal for plain numbers and normal strings that have no $ ambiguity.
PieceRequired?Role
<value>YesConstant to return unchanged
String with $Common caseStops field-path parsing
Number / bool / nullOptional wrapMakes “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 }

Cheat Sheet

Keep these facts handy when embedding constants in pipelines.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $literal: <value> }
Main jobReturn a constant; skip field-path parsing
Classic trap"$price" vs { $literal: "$price" }
Acceptsstring, number, bool, null, array, object
Not needed forMost plain numbers / non-$ strings
Sample{ $literal: "active" } → "active"

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

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"

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.

mongosh
db.products.insertOne({
  _id: 1,
  item: "Notebook",
  price: 50
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in products. Run find() to see it:

mongosh
db.products.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id
item"Notebook"Product name
price50Unit price
Picture
"$price"                 → 50   (field)
{ $literal: "$price" }   → "$price" (text)
{ $literal: "active" }   → "active" (label)

How It Works

  1. Insert once. insertOne adds a single product to products.
  2. Keep a real price field. Later we will contrast reading it vs printing "$price".
  3. find() shows the same shape. Ready for Example 2.

Example 2 — Constant Status

Goal: add a fixed status of "active" to every product using $literal.

mongosh
db.products.aggregate([
  {
    $addFields: {
      status: { $literal: "active" }
    }
  }
]);

How It Works

  1. Same stored document. Still using the Notebook from Example 1.
  2. Wrap the constant. { $literal: "active" } means the plain string, not a field.
  3. Write the field. $addFields adds status: "active".

Example 3 — Protect a $ String

Same product, but now we want the text "$price", not the number from the price field. Compare both sides.

mongosh
db.products.aggregate([
  {
    $addFields: {
      asField: "$price",
      asText: { $literal: "$price" }
    }
  }
]);

How It Works

  1. Field path. "$price" reads the document field → 50.
  2. Literal string. { $literal: "$price" } keeps the characters → "$price".
  3. Same characters, different meaning. That is why $literal exists.

Edge Cases & Pitfalls

Check these before wrapping every constant in $literal.

$

Strings that start with $

Without $literal, "$price" is a field path. Wrap it when you want the text itself.

need

Not every constant needs it

Plain numbers and normal strings like "active" usually work without wrapping. Use $literal when intent or $ ambiguity matters.

vs let

$literal ≠ $let

$literal freezes a value. $let names a computed expression for reuse with $$vars.

objects

Whole objects stay constant

{ $literal: { a: 1 } } returns that object as data. Keys inside are not evaluated as expressions.

Performance Notes

ApproachCostBest for
$literalTrivial constantFixed labels / $ strings
Plain constantSameNumbers / non-$ strings
Field path "$x"Field readDocument values

$literal is extremely cheap. Prefer it for clarity when a value could be misread as a field path; otherwise a plain constant is fine.

Key Takeaways

  • Role: return a constant; skip field-path parsing.
  • Classic use: keep the string "$price" as text.
  • Optional wrap: plain numbers often need no $literal.
  • Sample: "$price" → 50 · $literal "$price" → "$price".

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.

Next: $ln

Compute the natural logarithm of a number in aggregation.

$ln tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful