Navigation
This version of the documentation is archived and no longer supported.

Create a Sparse Index

Sparse indexes are like non-sparse indexes, except that they omit references to documents that do not include the indexed field. For fields that are only present in some documents sparse indexes may provide a significant space savings. See Sparse Indexes for more information about sparse indexes and their use.

See also

Index Concepts and Indexing Tutorials for more information.

Prototype

To create a sparse index on a field, use an operation that resembles the following prototype:

db.collection.createIndex( { a: 1 }, { sparse: true } )

Example

The following operation, creates a sparse index on the users collection that only includes a document in the index if the twitter_name field exists in a document.

db.users.createIndex( { twitter_name: 1 }, { sparse: true } )

The index excludes all documents that do not include the twitter_name field.

Considerations

Note

Sparse indexes can affect the results returned by the query, particularly with respect to sorts on fields not included in the index. See the sparse index section for more information.