i

Type of Indexes

MongoDB offers different index types to support specific data and query types.

1 Single Field Index

MongoDB supports indexes defined by the user, such as the single field index. We use a single field index to create an index on the single field of a document, and MongoDB can traverse in ascending and descending order. That's why the index key, in this case, doesn't matter.

Example: We can create an index on the lib_id field in ascending order. As this is a single field index, there will be no effect on the order. 

1.2 Compound Index

The compound index means an index of multiple fields. MongoDB supports user-defined indexes in multiple fields. The order of fields available in a compound index has significance. Suppose, if a compound index consists of {A: 1, B: -1}, the index sorts first by A and then, within each A value, sorts by B. To create a compound index use an operation that resembles the following prototype:

Syntax:

db.collection.createIndex( { : , : , ... } )

Example:

The following operation creates an ascending index both on the lib_id and balance fields. The order of the fields associated with a compound index is essential. The index will contain references to documents first sorted by the lib_ Id field values and, within each value of the lib_id field, sorted by values of the balance field.

1.3 Multikey Index

Incase of indexing the content of arrays,  MongoDB uses multikey indexes. If we index a field holding an array value, for each element of the array, MongoDB creates separate index entries. These multi-key indexes allow queries to select documents containing arrays by matching elements or array elements. If there is an array value in the indexed field, MongoDB can automatically determine whether to create a multikey index. We even do not need to specify the type of multikey explicitly. To create a multikey index, use the db.collection.createIndex() method:

Syntax:

db.collection.createIndex( { : < 1 or -1 > } )

Example:

We want to create an index on the array field tags of the book collection. The bellow code snippet will help us to create a multi-key Index.

8.3.4 Geospatial Index

MongoDB supports two types of indexes to query geospatial data – 2d indexes and 2d sphere indexes. When returning results, 2d indexes use planar geometry, and 2dsphere indexes use spherical geometry to yield results.

8.3.5 Text Index

We use text index to support searching for string content in a collection. Such index types do not store words that are language-specific ("a", "the", "or"). In a collection, text indexes restrict words to store root words only.

Syntax:

To create a text index, we use the same db.collection.createIndex() method. To index a field containing a string or an array of string elements, include the field and specify the string literal "text" in the index document, as in the following example:

db.collection.createIndex( { comments: "text" } )

 

Example:

Documents in address collection contain a string field "name". We want to create an index on this field. The below operation will help us to do so:

8.3.6 Hashed Indexes

To support hash-based sharding, MongoDB provides a hashed index that indexes the hash of the value of a field. Such indexes and their range have a more random distribution of values, but only support equality matches and cannot support range-based queries.