0
0
MongoDBquery~10 mins

Projection for selecting fields in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Projection for selecting fields
Start with full documents
Specify fields to include or exclude
MongoDB applies projection
Return documents with only selected fields
Projection lets you pick which fields to show from documents by including or excluding them.
Execution Sample
MongoDB
db.users.find({}, {name: 1, age: 1, _id: 0})
This query returns only the name and age fields from all user documents, hiding the _id field.
Execution Table
StepInput DocumentProjection SpecOutput Document
1{"_id": 1, "name": "Alice", "age": 30, "city": "NY"}{"name": 1, "age": 1, "_id": 0}{"name": "Alice", "age": 30}
2{"_id": 2, "name": "Bob", "age": 25, "city": "LA"}{"name": 1, "age": 1, "_id": 0}{"name": "Bob", "age": 25}
3{"_id": 3, "name": "Carol", "age": 27, "city": "Chicago"}{"name": 1, "age": 1, "_id": 0}{"name": "Carol", "age": 27}
4No more documentsN/AQuery ends
💡 All documents processed with specified projection, query ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Input DocumentFull document{"_id":1,"name":"Alice","age":30,"city":"NY"}{"_id":2,"name":"Bob","age":25,"city":"LA"}{"_id":3,"name":"Carol","age":27,"city":"Chicago"}No more documents
Projection Spec{"name":1,"age":1,"_id":0}{"name":1,"age":1,"_id":0}{"name":1,"age":1,"_id":0}{"name":1,"age":1,"_id":0}N/A
Output DocumentN/A{"name":"Alice","age":30}{"name":"Bob","age":25}{"name":"Carol","age":27}Query ends
Key Moments - 3 Insights
Why does the _id field disappear in the output even though it exists in the input?
Because the projection explicitly sets _id to 0 (exclude), MongoDB removes it from the output as shown in execution_table rows 1-3.
Can you mix including and excluding fields in the same projection?
No, except for _id, you must either include fields (set to 1) or exclude fields (set to 0). Here we include name and age, exclude _id, consistent in all rows.
What happens if a field is not mentioned in the projection?
Fields not included in the projection are excluded by default when using inclusion style, so city is missing in output documents as seen in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what fields are present in the output document?
Aname, age, and city
Bname and age only
Call fields including _id
Donly _id
💡 Hint
Check the Output Document column at Step 2 in execution_table.
At which step does the query finish processing all documents?
AStep 3
BStep 1
CStep 4
DStep 2
💡 Hint
Look for 'No more documents' in Input Document column in execution_table.
If we remove '_id: 0' from the projection, what changes in the output documents?
A_id field will be included in output
B_id field will be excluded
Cname and age will be excluded
DAll fields will be included
💡 Hint
Refer to key_moments about excluding _id and execution_table output documents.
Concept Snapshot
MongoDB Projection Syntax:
find(query, {field1: 1, field2: 1, _id: 0})
Includes only specified fields, excludes others.
_id is included by default; set _id:0 to hide it.
Cannot mix inclusion and exclusion except for _id.
Projection controls which fields appear in results.
Full Transcript
Projection in MongoDB lets you select which fields to show from documents. You write a query with an empty filter {} to get all documents, then add a projection object like {name:1, age:1, _id:0} to include name and age fields but hide the _id field. MongoDB processes each document, applies the projection, and returns only the selected fields. Fields not included are excluded by default when using inclusion style. You cannot mix including and excluding fields except for the _id field. This example shows three documents with full fields, and after projection, only name and age remain. The _id field disappears because we set it to 0 in the projection. The query ends after all documents are processed.