Partial indexes with filter in MongoDB - Time & Space Complexity
When using partial indexes with filters in MongoDB, we want to know how fast queries run as data grows.
We ask: How does the filter affect the number of operations MongoDB does?
Analyze the time complexity of the following MongoDB partial index creation and query.
db.orders.createIndex(
{ status: 1 },
{ partialFilterExpression: { status: { $eq: "pending" } } }
)
db.orders.find({ status: "pending" })
This code creates an index only on documents where status is "pending" and then queries those documents.
Look for repeated work MongoDB does when using this partial index.
- Primary operation: Scanning the partial index entries matching the filter.
- How many times: Once per matching document with status "pending".
As the number of "pending" documents grows, the work grows too, but only for those documents.
| Input Size (m) | Approx. Operations |
|---|---|
| 10 (pending docs) | About 10 index lookups |
| 100 (pending docs) | About 100 index lookups |
| 1000 (pending docs) | About 1000 index lookups |
Pattern observation: The cost grows linearly with the number of documents matching the filter, not total documents.
Time Complexity: O(m)
This means the query time grows in a straight line with the number of documents that match the filter, not all documents.
[X] Wrong: "The partial index speeds up queries on all documents equally."
[OK] Correct: The partial index only covers documents matching the filter, so queries on other documents don't benefit from it.
Understanding how partial indexes limit work to relevant data shows you can write efficient queries and indexes that scale well.
"What if we changed the filter to include more fields? How would that affect the time complexity?"
Practice
partial index in MongoDB?Solution
Step 1: Understand what partial indexes do
Partial indexes only include documents that meet a filter condition, unlike normal indexes that include all documents.Step 2: Compare options
To index only documents that match a specific filter condition correctly describes this behavior. Options A, B, and D describe other unrelated or incorrect behaviors.Final Answer:
To index only documents that match a specific filter condition -> Option AQuick Check:
Partial index = filtered indexing [OK]
- Thinking partial indexes index all documents
- Confusing partial indexes with backups
- Assuming partial indexes disable indexing
status field where status equals 'active'?Solution
Step 1: Recall the correct option name for partial indexes
The option to specify a filter for partial indexes ispartialFilterExpression.Step 2: Match syntax with options
db.collection.createIndex({status: 1}, {partialFilterExpression: {status: 'active'}}) usespartialFilterExpressioncorrectly. Options B, C, and D use incorrect option names or methods.Final Answer:
db.collection.createIndex({status: 1}, {partialFilterExpression: {status: 'active'}}) -> Option BQuick Check:
Use partialFilterExpression for partial indexes [OK]
- Using 'filter' instead of partialFilterExpression
- Trying to use createPartialIndex method (doesn't exist)
- Using partialIndex option instead of partialFilterExpression
db.users.createIndex({age: 1}, {partialFilterExpression: {age: {$gte: 18}}})Which documents will be included in this index?
Solution
Step 1: Understand the filter condition
The filter{age: {$gte: 18}}means age greater than or equal to 18.Step 2: Determine which documents match
Only documents with age 18 or older satisfy this condition and will be indexed.Final Answer:
Only documents where age is 18 or older -> Option CQuick Check:
Filter {$gte: 18} means age ≥ 18 [OK]
- Thinking all documents are indexed
- Confusing $gte with $lt
- Assuming only exact matches are indexed
db.orders.createIndex({price: 1}, {partialFilterExpression: {price: {$gt: 100}}})But you get an error. What is the most likely cause?
Solution
Step 1: Check syntax correctness
The syntax shown is correct ifpartialFilterExpressionis spelled and placed properly.Step 2: Identify common mistakes
Errors often happen ifpartialFilterExpressionis misspelled or placed outside the options object.Final Answer:
The partialFilterExpression option is misspelled or misplaced -> Option DQuick Check:
Check spelling and placement of partialFilterExpression [OK]
- Misspelling partialFilterExpression
- Placing filter outside options object
- Assuming comparison operators are unsupported
products where only documents with stock greater than zero are frequently queried. Which partial index creation command is best?Solution
Step 1: Identify the filter condition for partial index
The goal is to index only documents wherestockis greater than zero.Step 2: Match the correct syntax and filter
db.products.createIndex({stock: 1}, {partialFilterExpression: {stock: {$gt: 0}}}) usespartialFilterExpressionwith{stock: {$gt: 0}}, which matches the requirement.Step 3: Evaluate other options
db.products.createIndex({stock: 1}) indexes all documents (not partial). db.products.createIndex({stock: 1}, {partialFilterExpression: {stock: 0}}) filters only stock equal to zero, which is wrong. db.products.createIndex({stock: 1}, {filter: {stock: {$gt: 0}}}) uses incorrect option namefilter.Final Answer:
db.products.createIndex({stock: 1}, {partialFilterExpression: {stock: {$gt: 0}}}) -> Option AQuick Check:
Use partialFilterExpression with correct filter [OK]
- Indexing all documents instead of partial
- Using wrong filter values
- Using incorrect option names like 'filter'
Ready to prove it?
Pass the test and this pattern moves unknown
