$literal (Literal)

Last updated: 2026-10-09

$literal (Literal)

$literal returns a value without parsing it as an expression. It is used when a string starting with $ should stay plain text, or when a numeric literal must keep its exact type. For example { $project: { prefix: { $literal: '$' } } } outputs the string '$', while writing '$' directly would be treated as a field path. $literal is also handy for literal boolean or numeric arguments inside $cond or $switch. It is the most concise way to emit a constant in $project.

CategoryLiteral, Variable & Object
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $literal: <value> }

⚙️ Parameters

valueAny value or expression; strings starting with $ are treated literally.

Returns:The value passed in, returned as-is without expression parsing

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $literal: emit string constants starting with $
db.orders.aggregate([
  { $project: {
      currency: { $literal: '$' },
      rate: { $literal: 0.08 }
  } }
])

Output:

{ '_id': 1, 'currency': '$', 'rate': 0.08 }
{ '_id': 2, 'currency': '$', 'rate': 0.08 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat is $literal for?
AEmit constants such as $ strings without them being parsed as field paths.
QWhat happens without $literal?
AA $ string would be parsed as a field path and resolve to null when the field is absent.
QCan $literal wrap an expression?
AIt would not evaluate it; the literal content is returned as-is.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏