$project (Projection)

Last updated: 2026-10-09

$project (Projection)

$project is the projection stage: it selects or excludes fields, or computes new ones, mapping each document to a new shape. Unlike $addFields, it outputs only _id and explicitly listed fields by default: 1 includes, 0 excludes, and an expression creates a new field. Use it to shrink documents, rename fields, or flatten embedded objects. Inclusion and exclusion cannot be mixed (except _id).

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

📝 Syntax

{ $project: { <field>: <1 or 0 or expression>, ... } }

⚙️ Parameters

field projectionField names mapped to 1 (include), 0 (exclude), or an aggregation expression (computed field).

Returns:Projected document stream

▶ Example

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

// $project: keep name, compute total
db.orders.aggregate([
  { $project: { _id: 0, name: 1, total: { $multiply: ['$price', '$qty'] } } }
])

Output:

// Each doc now has only name and total
{ "name": "Pen", "total": 150 }
{ "name": "Book", "total": 30 }

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat is the difference between $project and $addFields?
A$addFields keeps all original fields and adds new ones; $project drops unlisted fields by default, used to trim or rebuild the document shape.
QCan $project exclude multiple fields at once?
AYes, set them all to 0, e.g. { $project: { secret: 0, log: 0 } }, which keeps every other field.
QDoes $project change field order?
AOutput fields follow the order written in the projection, regardless of the original document order.
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%

🙏 帮我们做得更好

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

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