Excluding fields from results in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we ask a database to exclude certain fields from the results, it needs to check each item to remove those parts.
We want to know how the time it takes changes as the number of items grows.
Analyze the time complexity of the following code snippet.
db.collection.find({}, { password: 0, secretKey: 0 })
This code fetches all documents but leaves out the password and secretKey fields from each result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database scans each document to remove the specified fields.
- How many times: Once for every document returned.
As the number of documents grows, the database must process more items to exclude fields.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 field removals |
| 100 | 100 field removals |
| 1000 | 1000 field removals |
Pattern observation: The work grows directly with the number of documents.
Time Complexity: O(n)
This means the time to exclude fields grows in a straight line as more documents are processed.
[X] Wrong: "Excluding fields makes the query instant no matter how many documents there are."
[OK] Correct: The database still looks at every document to remove fields, so more documents mean more work.
Understanding how excluding fields affects time helps you explain how queries scale in real projects.
"What if we added a filter to only return 10 documents instead of all? How would the time complexity change?"
Practice
0 in a MongoDB query projection do?Solution
Step 1: Understand projection in MongoDB
Projection controls which fields appear in the query results.Step 2: Interpret setting a field to 0
Setting a field to 0 means exclude that field from the results.Final Answer:
It excludes that field from the query results. -> Option AQuick Check:
Field set to 0 = excluded [OK]
- Thinking 0 includes the field
- Confusing exclusion with sorting
- Mixing exclusion and inclusion in projection
password from the results in a MongoDB query?Solution
Step 1: Identify correct projection syntax
Projection is the second argument to find(), with field names and 0 or 1 values.Step 2: Exclude field by setting it to 0
Setting password: 0 excludes it from results.Final Answer:
db.users.find({}, {password: 0}) -> Option AQuick Check:
Exclude field = field: 0 in projection [OK]
- Using 1 instead of 0 to exclude
- Putting projection inside query filter
- Using non-existent 'exclude' keyword
products with documents like {_id: 1, name: 'Pen', price: 5, stock: 100}, what will be the result of db.products.find({}, {price: 0, stock: 0})?Solution
Step 1: Understand projection excludes price and stock
Setting price: 0 and stock: 0 excludes these fields from results.Step 2: Check which fields remain
_id is included by default, name is included as not excluded.Final Answer:
[{_id: 1, name: 'Pen'}] -> Option DQuick Check:
Excluded fields missing, others present [OK]
- Expecting excluded fields to appear
- Forgetting _id is included by default
- Assuming all fields are excluded
email and include name fields: db.users.find({}, {email: 0, name: 1})?Solution
Step 1: Recall projection rules
MongoDB does not allow mixing inclusion and exclusion in projection except for _id.Step 2: Analyze the query
Query mixes email: 0 (exclude) and name: 1 (include), which is invalid.Final Answer:
You cannot mix exclusion and inclusion except for _id. -> Option CQuick Check:
Mixing 0 and 1 fields (except _id) = error [OK]
- Mixing 0 and 1 in projection
- Assuming syntax is always flexible
- Using 'exclude' keyword which doesn't exist
orders collection and exclude creditCardNumber and cvv fields for security, but include all other fields including _id. Which query achieves this correctly?Solution
Step 1: Exclude sensitive fields by setting them to 0
Setting creditCardNumber and cvv to 0 excludes them from results.Step 2: Keep _id included by default
Not excluding _id means it stays included, as required.Final Answer:
db.orders.find({}, {creditCardNumber: 0, cvv: 0}) -> Option BQuick Check:
Exclude sensitive fields only, keep others including _id [OK]
- Excluding _id unintentionally
- Using 1 to exclude fields
- Using false instead of 0
