Projections — MongoDB Technical Interview Questions & Answers
Choosing which fields find() returns.
Projections
- Simple meaning: Projection picks which fields come back, so you do not download the whole document.
- How to use: db.students.find({ city: "Pune" }, { name: 1, marks: 1, _id: 0 }). 1 = include, 0 = exclude.
- You usually cannot mix include and exclude except for _id.
- Try this: find with only name. Check _id still appears unless _id: 0.
- Common mistake: { name: 1, city: 0 } mixed projection error.
- Interview tip: Second argument of find. _id is included unless you exclude it.
Technical Interview Tip: Show { name: 1, _id: 0 }.
Code Examples for Projections
Projections — working example
db.users.find(
{ active: true },
{ name: 1, email: 1, _id: 0 }
)Projections — projection (2nd argument) includes (1) or excludes (0) fields — do not mix include/exclude except for _id.
How to Explain Projections to an Interviewer
How do you hide _id?
Add _id: 0 in the projection