$project
$project: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 54 of 100
$project
Foundations & CRUD ✓ → Queries & Schema ✓ → Aggregation & Scale → Atlas & Projects
Aggregation & Scale · 3 — Pipelines · ~10 min · MongoDB — Aggregation Pipelines
What is this?
$project reshapes each document — include, exclude, rename, and compute new fields. Later stages see only what you projected.
Why should you care?
APIs and reports rarely need every field. Computing fullName or lineTotal in the pipeline keeps app code thinner.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.users.insertOne({ first: "Asha", last: "Nair", passwordHash: "x", city: "Kochi" })
db.users.aggregate([
{ $project: {
_id: 0,
name: { $concat: ["$first", " ", "$last"] },
city: 1
}}
])
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- passwordHash is dropped by omission.
- name is computed with $concat.
- city is kept.
- _id: 0 hides the id.
Practice next
- Run the pipeline.
- Add a field isCoastal: { $eq: ["$city", "Kochi"] }.
- Contrast with find projection — aggregation expressions are richer.
- Compute priceWithTax: { $multiply: ["$price", 1.18] } on products.
- Rename total to amount with total: 0, amount: "$total" patterns carefully.
Remember
$project shapes fields. You can compute new values. Drop secrets early.
Export users for email tool
Marketing export projects email and name only.
Outcome: CSV never contains password hashes.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!