$addFields (Add Fields)

Last updated: 2026-10-09

$addFields (Add Fields)

$addFields adds new fields to, or overwrites existing fields in, each document while preserving all others. Unlike $project — which drops unlisted fields by default — $addFields only grows the document. New values can be any aggregation expression, often computed from $lookup results. Since MongoDB 3.4 it is equivalent to $set.

CategoryCore Stages
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $addFields: { <newField>: <expression>, ... } }

⚙️ Parameters

new fieldField names mapped to expressions that reference fields of the input document.

Returns:Document stream with added or overwritten fields

▶ Example

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

// $addFields: add a total field
db.sales.aggregate([
  { $addFields: { total: { $multiply: ['$price', '$qty'] } } }
])

Output:

// All original fields kept, plus total
{ "item": "Pen", "price": 50, "qty": 3, "total": 150 }

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat is the difference between $addFields and $set?
ASince MongoDB 3.4 they are fully equivalent; $addFields is the older name.
QCan $addFields reference its new fields?
ANo. Expressions in one stage see only the input document; chain multiple $addFields stages instead.
QDoes $addFields overwrite existing fields?
AYes. A field with an existing name is overwritten.
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%

🙏 帮我们做得更好

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

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