Recall & Review
beginner
What is a partial index in MongoDB?
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?
✗ 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?
✗ Incorrect
The option partialFilterExpression defines the filter condition for a partial index.
If a query filters documents outside the partial index filter, what happens?
✗ 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?
✗ Incorrect
Partial indexes reduce index size and improve write performance by indexing fewer documents.
Which of these is a valid partial index filter expression?
✗ 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.