0
0
MongoDBquery~10 mins

Partial indexes with filter in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Partial indexes with filter
Start: Define Partial Index
Specify Filter Condition
Create Index on Filtered Documents
Query Uses Partial Index if Filter Matches
Faster Query on Subset of Data
End
Create an index only on documents matching a filter. Queries using that filter can use the smaller index for faster results.
Execution Sample
MongoDB
db.collection.createIndex(
  { status: 1 },
  { partialFilterExpression: { status: { $eq: "active" } } }
)
Creates an index on the 'status' field only for documents where status is 'active'.
Execution Table
StepActionFilter ConditionIndex ContentQuery ExampleIndex Used?
1Create partial indexstatus == 'active'Indexes only docs with status='active'N/AN/A
2Insert doc {status:'active', name:'A'}N/ADoc included in indexN/AN/A
3Insert doc {status:'inactive', name:'B'}N/ADoc NOT included in indexN/AN/A
4Query {status:'active'}status=='active'Index covers matching docsUses partial indexYes
5Query {status:'inactive'}status=='inactive'Index does not cover these docsDoes not use partial indexNo
6Query {name:'A'}No filter on statusIndex not usefulDoes not use partial indexNo
7EndN/AN/AN/AN/A
💡 No more steps; partial index only covers documents matching filter condition.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Index Contentempty{status:'active', name:'A'}{status:'active', name:'A'}used for status='active' queriesnot used for status='inactive' queriespartial index covers only active docs
Key Moments - 2 Insights
Why doesn't the partial index include documents where status is 'inactive'?
Because the partialFilterExpression only includes documents where status equals 'active' as shown in execution_table step 1 and step 3 where inactive docs are not indexed.
Can queries without the filter condition use the partial index?
No, queries like {name:'A'} do not match the filter condition, so the partial index is not used (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is a document NOT included in the partial index?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Check the 'Index Content' column in step 3 where the inactive document is not included.
At which query does MongoDB use the partial index?
AQuery {status:'active'}
BQuery {status:'inactive'}
CQuery {name:'A'}
DQuery {age:30}
💡 Hint
See 'Index Used?' column in execution_table step 4.
If the filter condition changed to status=='inactive', which document would be indexed?
ADocument with status 'active'
BDocument with status 'inactive'
CBoth documents
DNo documents
💡 Hint
Partial index only includes documents matching the filter condition, see execution_table step 1.
Concept Snapshot
Partial indexes create an index only on documents matching a filter.
Syntax: createIndex({field:1}, {partialFilterExpression: {field: condition}})
Queries matching the filter use the smaller index for faster results.
Documents not matching filter are not indexed.
Useful to optimize queries on subsets of data.
Full Transcript
Partial indexes in MongoDB let you create an index only on documents that match a filter condition. For example, you can index only documents where status is 'active'. When you create the index, MongoDB scans documents and includes only those matching the filter. Queries that use the filter condition can use this smaller index, making queries faster. Documents that don't match the filter are not included in the index and queries on those documents won't use the partial index. This helps optimize performance when you only query a subset of your data often.