A partial index is an index built only on the documents in a collection that meet a specified filter condition. It helps improve performance and reduce index size by indexing only relevant documents.
Click to reveal answer
beginner
How do you create a partial index with a filter in MongoDB?
Use the createIndex() method with the option { partialFilterExpression: <filter> }, where <filter> defines the condition documents must meet to be included in the index.
Click to reveal answer
intermediate
Why use a partial index instead of a full index?
Partial indexes save space and improve write performance by indexing only documents that match the filter, making queries on those documents faster without indexing the entire collection.
Click to reveal answer
intermediate
Example: What does this partial index do?
{ age: 1 }, { partialFilterExpression: { age: { $gte: 18 } } }
This index only includes documents where the age field is 18 or older. Queries filtering on age >= 18 will be faster, but documents with age less than 18 are not indexed.
Click to reveal answer
intermediate
Can a partial index speed up queries that do not match the filter condition?
No. Partial indexes only help queries that match the filter condition. Queries outside the filter will not use the partial index and may scan the collection.
Click to reveal answer
What does a partial index in MongoDB do?
AIndexes only documents matching a filter condition
BIndexes all documents in a collection
CIndexes documents randomly
DIndexes documents based on size
✗ Incorrect
A partial index only includes documents that match the specified filter condition.
Which option is used to specify the filter condition when creating a partial index?
AfilterCondition
BindexFilter
CpartialFilterExpression
DfilterExpression
✗ Incorrect
The option partialFilterExpression defines the filter condition for a partial index.
If a query filters documents outside the partial index filter, what happens?
AThe query uses a random index
BThe query fails
CThe partial index is used anyway
DThe query scans the whole collection
✗ Incorrect
Queries outside the partial index filter do not use the partial index and scan the collection.
What is a benefit of using partial indexes?
AThey reduce index size and improve write speed
BThey make queries slower
CThey index all fields automatically
DThey delete documents automatically
✗ Incorrect
Partial indexes reduce index size and improve write performance by indexing fewer documents.
Which of these is a valid partial index filter expression?
A{ name: 'John' }
BAll of the above
C{ status: { $exists: true } }
D{ age: { $lt: 30 } }
✗ Incorrect
Any valid MongoDB query filter can be used as a partialFilterExpression.
Explain what a partial index is and why you might use one in MongoDB.
Think about indexing only some documents instead of all.
You got /3 concepts.
Describe how to create a partial index with a filter and give an example.
Remember the option name partialFilterExpression.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a partial index in MongoDB?
easy
A. To index only documents that match a specific filter condition
B. To index all documents in a collection
C. To create a backup of the index
D. To speed up writes by disabling indexing
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 A
Quick Check:
Partial index = filtered indexing [OK]
Hint: Partial indexes filter documents before indexing [OK]
Common Mistakes:
Thinking partial indexes index all documents
Confusing partial indexes with backups
Assuming partial indexes disable indexing
2. Which of the following is the correct syntax to create a partial index on the status field where status equals 'active'?
easy
A. db.collection.createIndex({status: 1}, {filter: {status: 'active'}})
B. db.collection.createIndex({status: 1}, {partialFilterExpression: {status: 'active'}})
C. db.collection.createPartialIndex({status: 1}, {status: 'active'})
D. db.collection.createIndex({status: 1}, {partialIndex: {status: 'active'}})
Solution
Step 1: Recall the correct option name for partial indexes
The option to specify a filter for partial indexes is partialFilterExpression.
Step 2: Match syntax with options
db.collection.createIndex({status: 1}, {partialFilterExpression: {status: 'active'}}) uses partialFilterExpression correctly. Options B, C, and D use incorrect option names or methods.
Final Answer:
db.collection.createIndex({status: 1}, {partialFilterExpression: {status: 'active'}}) -> Option B
Quick Check:
Use partialFilterExpression for partial indexes [OK]
Hint: Use partialFilterExpression option to filter index documents [OK]
Common Mistakes:
Using 'filter' instead of partialFilterExpression
Trying to use createPartialIndex method (doesn't exist)
Using partialIndex option instead of partialFilterExpression
But you get an error. What is the most likely cause?
medium
A. The filter expression uses an unsupported operator
B. The field name in the index is missing
C. Partial indexes cannot use comparison operators
D. The partialFilterExpression option is misspelled or misplaced
Solution
Step 1: Check syntax correctness
The syntax shown is correct if partialFilterExpression is spelled and placed properly.
Step 2: Identify common mistakes
Errors often happen if partialFilterExpression is misspelled or placed outside the options object.
Final Answer:
The partialFilterExpression option is misspelled or misplaced -> Option D
Quick Check:
Check spelling and placement of partialFilterExpression [OK]
Hint: Check spelling and position of partialFilterExpression [OK]
Common Mistakes:
Misspelling partialFilterExpression
Placing filter outside options object
Assuming comparison operators are unsupported
5. You want to speed up queries on a collection products where only documents with stock greater than zero are frequently queried. Which partial index creation command is best?
hard
A. db.products.createIndex({stock: 1}, {partialFilterExpression: {stock: {$gt: 0}}})
B. db.products.createIndex({stock: 1})
C. db.products.createIndex({stock: 1}, {partialFilterExpression: {stock: 0}})
D. db.products.createIndex({stock: 1}, {filter: {stock: {$gt: 0}}})
Solution
Step 1: Identify the filter condition for partial index
The goal is to index only documents where stock is greater than zero.
Step 2: Match the correct syntax and filter
db.products.createIndex({stock: 1}, {partialFilterExpression: {stock: {$gt: 0}}}) uses partialFilterExpression with {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 name filter.
Final Answer:
db.products.createIndex({stock: 1}, {partialFilterExpression: {stock: {$gt: 0}}}) -> Option A
Quick Check:
Use partialFilterExpression with correct filter [OK]
Hint: Use partialFilterExpression with correct filter condition [OK]
Common Mistakes:
Indexing all documents instead of partial
Using wrong filter values
Using incorrect option names like 'filter'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.