Subset pattern for large documents in MongoDB - Time & Space Complexity
When working with large documents in MongoDB, fetching only needed parts helps save time.
We want to know how the time to get data changes when we ask for just a subset of fields.
Analyze the time complexity of the following MongoDB query using projection.
db.collection.find(
{ status: "active" },
{ name: 1, email: 1, _id: 0 }
).toArray()
This query finds documents with status "active" and returns only the name and email fields.
Look at what repeats as the query runs:
- Primary operation: Scanning documents matching the filter.
- How many times: Once per matching document.
- Projection step: Extracting only requested fields from each document.
- How many times: Also once per matching document.
As the number of matching documents grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 document scans + 10 projections |
| 100 | 100 document scans + 100 projections |
| 1000 | 1000 document scans + 1000 projections |
Pattern observation: The total work grows roughly in direct proportion to the number of matching documents.
Time Complexity: O(n)
This means the time to run the query grows linearly with the number of documents that match the filter.
[X] Wrong: "Selecting fewer fields makes the query run in constant time regardless of data size."
[OK] Correct: Even if you ask for fewer fields, MongoDB still scans each matching document once, so time grows with the number of matches.
Understanding how projection affects query time shows you know how to handle big data efficiently and write queries that scale well.
"What if we added an index on the status field? How would that change the time complexity?"