0
0
MongoDBquery~5 mins

Partial indexes with filter in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AIndexes only documents matching a filter condition
BIndexes all documents in a collection
CIndexes documents randomly
DIndexes documents based on size
Which option is used to specify the filter condition when creating a partial index?
AfilterCondition
BindexFilter
CpartialFilterExpression
DfilterExpression
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
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
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 } }
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.