Bird
Raised Fist0
MongoDBquery~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does projection do in a MongoDB query?
easy
A. It selects which fields to include or exclude in the query result.
B. It sorts the documents in the collection.
C. It updates the documents in the collection.
D. It deletes documents from the collection.

Solution

  1. Step 1: Understand the purpose of projection

    Projection in MongoDB is used to specify which fields to include or exclude in the output of a query.
  2. Step 2: Differentiate projection from other operations

    Sorting, updating, and deleting are different operations and not related to projection.
  3. Final Answer:

    It selects which fields to include or exclude in the query result. -> Option A
  4. Quick Check:

    Projection = Select fields [OK]
Hint: Projection picks fields to show, not to sort or update [OK]
Common Mistakes:
  • Confusing projection with sorting
  • Thinking projection updates data
  • Assuming projection deletes documents
2. Which of the following is the correct syntax to include only the name and age fields in a MongoDB find query?
easy
A. { name: 1, age: 1 }
B. { name: true, age: true }
C. { name: 'include', age: 'include' }
D. { name: 0, age: 0 }

Solution

  1. Step 1: Recall projection syntax

    In MongoDB, to include fields, you set them to 1 in the projection document.
  2. Step 2: Check each option

    { name: 1, age: 1 } uses 1 correctly; { name: true, age: true } uses true which is valid in MongoDB; { name: 'include', age: 'include' } uses strings which is invalid; { name: 0, age: 0 } excludes fields instead of including.
  3. Final Answer:

    { name: 1, age: 1 } -> Option A
  4. Quick Check:

    Include fields = 1 [OK]
Hint: Use 1 to include fields, not strings [OK]
Common Mistakes:
  • Using strings like 'include'
  • Using 0 to include fields
3. Given the collection documents:
{ _id: 1, name: "Alice", age: 25, city: "NY" }
{ _id: 2, name: "Bob", age: 30, city: "LA" }

What will be the result of the query db.collection.find({}, { name: 1, city: 1 })?
medium
A. [{ _id: 1, age: 25 }, { _id: 2, age: 30 }]
B. [{ name: "Alice", city: "NY" }, { name: "Bob", city: "LA" }]
C. [{ _id: 1, name: "Alice", city: "NY" }, { _id: 2, name: "Bob", city: "LA" }]
D. [{ name: "Alice", age: 25, city: "NY" }, { name: "Bob", age: 30, city: "LA" }]

Solution

  1. Step 1: Understand default _id behavior

    By default, MongoDB includes the _id field unless explicitly excluded.
  2. Step 2: Check projection fields

    The query includes name and city fields with 1, so these fields plus _id will appear.
  3. Final Answer:

    [{ _id: 1, name: "Alice", city: "NY" }, { _id: 2, name: "Bob", city: "LA" }] -> Option C
  4. Quick Check:

    Projection includes fields + _id by default [OK]
Hint: Projection includes _id unless set to 0 [OK]
Common Mistakes:
  • Assuming _id is excluded by default
  • Expecting age field in result
  • Confusing included and excluded fields
4. What is wrong with this MongoDB query to exclude the password field?
db.users.find({}, { password: 1 })
medium
A. The syntax for projection is incorrect.
B. It includes the password field instead of excluding it.
C. You cannot exclude fields in MongoDB projection.
D. The query will return an error because password is a reserved word.

Solution

  1. Step 1: Understand projection values

    Setting a field to 1 includes it; setting to 0 excludes it.
  2. Step 2: Analyze the query

    The query sets password: 1, so it includes the password field (and _id), not excludes it.
  3. Final Answer:

    It includes the password field instead of excluding it. -> Option B
  4. Quick Check:

    Use 0 to exclude fields [OK]
Hint: Use 0 to exclude fields, 1 to include [OK]
Common Mistakes:
  • Using 1 to exclude fields
  • Thinking password is reserved word
  • Believing exclusion is not possible
5. You want to fetch documents from a collection but exclude the _id and password fields while including all others. Which projection is correct?
hard
A. { _id: 1, password: 1 }
B. { _id: 1, password: 0 }
C. { _id: 0, password: 1 }
D. { _id: 0, password: 0 }

Solution

  1. Step 1: Understand exclusion in projection

    Setting fields to 0 excludes them from the result.
  2. Step 2: Apply exclusion to _id and password

    To exclude both _id and password, set both to 0 in the projection.
  3. Final Answer:

    { _id: 0, password: 0 } -> Option D
  4. Quick Check:

    Exclude fields with 0, include others by default [OK]
Hint: Set unwanted fields to 0, others stay included [OK]
Common Mistakes:
  • Mixing inclusion and exclusion in projection
  • Forgetting to exclude _id explicitly
  • Setting excluded fields to 1