Sparse indexes

A sparse index is similar to a partial index, preceding it by several years (it has been available since version 1.8).

A sparse index only indexes values that contain the following field:

> db.books.createIndex( { "price": 1 }, { sparse: true } )

It will only create an index with the documents that contain a price field.

Some indexes are always sparse, due to their nature:

  • 2d, 2dsphere (version 2)
  • geoHaystack
  • text

A sparse and unique index will allow for multiple documents missing the index key. It will not allow for documents with the same index field values. A sparse and compound index with geospatial indexes (2d, 2dsphere, and geoHaystack) will index the document as long as it has the geospatial field.

A sparse and compound index with the text field will index the document as long as it has the text field. A sparse and compound index without any of the two preceding cases will index the document as long as it has at least one of the fields.

Avoid creating new sparse indexes in the latest versions of MongoDB; use partial indexes instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset