- Indexes >
- Indexing Tutorials >
- Index Creation Tutorials >
- Create a Partial Index
Create a Partial Index¶
On this page
New in version 3.2.
Partial indexes only index the documents in a collection that meet a specified filter expression. By indexing a subset of the documents in a collection, partial indexes have lower storage requirements and reduced performance costs for index creation and maintenance. See Partial Indexes for more information about partial indexes and their use.
See also
Index Concepts and Indexing Tutorials for more information.
Prototype¶
To create a partial index on a field, use the partialFilterExpression option when creating the index, as in the following:
db.collection.createIndex(
{ a: 1 },
{ partialFilterExpression: { b: { $gt: 5 } } }
)
The partialFilterExpression option accepts a document that specifies the filter condition using:
Example¶
The following operation creates a sparse index on the users collection that only includes a document in the index if the archived field is false.
db.users.createIndex( { username: 1 }, { archived: false } )
The index only includes documents where the archived field is false.
Considerations¶
Note
To use the partial index, a query must contain the filter expression (or a modified filter expression that specifies a subset of the filter expression) as part of its query condition. As such, MongoDB will not use the partial index if the index results in an incomplete result set for the query or sort operation.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.