Projection for selecting fields in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we ask for only certain fields from documents in MongoDB, we use projection. Understanding how this affects time helps us know how fast queries run as data grows.
We want to see how the work changes when we select fewer or more fields.
Analyze the time complexity of the following code snippet.
db.collection.find(
{ status: "active" },
{ name: 1, email: 1, _id: 0 }
)
This query finds all documents with status "active" and returns only the name and email fields, excluding the _id field.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning matching documents and extracting selected fields.
- How many times: Once per matching document in the result set.
As the number of matching documents grows, the database must process each one to pick the requested fields.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 field extractions |
| 100 | 100 field extractions |
| 1000 | 1000 field extractions |
Pattern observation: The work grows directly with the number of matching documents.
Time Complexity: O(n)
This means the time to get the selected fields grows linearly with how many documents match the query.
[X] Wrong: "Selecting fewer fields makes the query run instantly regardless of data size."
[OK] Correct: Even if fewer fields are returned, the database still processes each matching document, so time grows with the number of matches.
Understanding how selecting fields affects query time shows you know how databases handle data efficiently. This skill helps you write queries that balance speed and needed information.
"What if we added an index on the status field? How would the time complexity change?"
Practice
Solution
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.Step 2: Differentiate projection from other operations
Sorting, updating, and deleting are different operations and not related to projection.Final Answer:
It selects which fields to include or exclude in the query result. -> Option AQuick Check:
Projection = Select fields [OK]
- Confusing projection with sorting
- Thinking projection updates data
- Assuming projection deletes documents
name and age fields in a MongoDB find query?Solution
Step 1: Recall projection syntax
In MongoDB, to include fields, you set them to 1 in the projection document.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.Final Answer:
{ name: 1, age: 1 } -> Option AQuick Check:
Include fields = 1 [OK]
- Using strings like 'include'
- Using 0 to include fields
{ _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 })?Solution
Step 1: Understand default _id behavior
By default, MongoDB includes the_idfield unless explicitly excluded.Step 2: Check projection fields
The query includesnameandcityfields with 1, so these fields plus_idwill appear.Final Answer:
[{ _id: 1, name: "Alice", city: "NY" }, { _id: 2, name: "Bob", city: "LA" }] -> Option CQuick Check:
Projection includes fields + _id by default [OK]
- Assuming _id is excluded by default
- Expecting age field in result
- Confusing included and excluded fields
password field?db.users.find({}, { password: 1 })Solution
Step 1: Understand projection values
Setting a field to 1 includes it; setting to 0 excludes it.Step 2: Analyze the query
The query setspassword: 1, so it includes the password field (and_id), not excludes it.Final Answer:
It includes the password field instead of excluding it. -> Option BQuick Check:
Use 0 to exclude fields [OK]
- Using 1 to exclude fields
- Thinking password is reserved word
- Believing exclusion is not possible
_id and password fields while including all others. Which projection is correct?Solution
Step 1: Understand exclusion in projection
Setting fields to 0 excludes them from the result.Step 2: Apply exclusion to _id and password
To exclude both_idandpassword, set both to 0 in the projection.Final Answer:
{ _id: 0, password: 0 } -> Option DQuick Check:
Exclude fields with 0, include others by default [OK]
- Mixing inclusion and exclusion in projection
- Forgetting to exclude _id explicitly
- Setting excluded fields to 1
