
Hashed index: To support hash-based sharding, MongoDB supports hashed indexes.

To create text indexes, we have to use the db.collection.createIndex() method, but we need to pass a text string literal in the query:Ĭompound indexes can be used with text indexes to define an ascending or descending order of the index. Text indexes: MongoDB provides text indexes that support the searching of string contents in the MongoDB collection.MongoDB automatically decides whether to create multi-key indexes or not. These indexes allow queries to select the document by matching the element or set of elements of the array. If you index the field that contains the array values, MongoDB creates an index for each field in the object of an array. Multikey index: MongoDB uses multi-key indexes to index the content in the array.For example, a compound index defined as will sort data by firstName first and then each firstName with age. The order of fields defined while creating an index has a significant effect. Compound index: MongoDB also supports user-defined indexes on multiple fields.To create a descending index, we have to provide -1 instead of 1. This will create an ascending index on the firstName field. The following is an example of creating an index on the single field where we are creating an index on the firstName field of the user_profiles collection: For a single field index, the order of the index does not matter as MongoDB can traverse indexes in any order. Single field: In addition to the _id field index, MongoDB allows the creation of an index on any single field in ascending or descending order.The following are the different types of indexes:

MongoDB indexes use the B-tree data structure. The preceding method creates an index only if an index with the same specification does not exist.

The following syntax is used to create an index in MongoDB: >db.collection.createIndex(, ) You cannot drop an index on an _id field. The _id index prevents users from inserting two documents with the same _id value. MongoDB creates the default _id index when creating a document. MongoDB defines indexes at the collection level and supports indexes on fields and sub-fields of documents. Indexes in MongoDB are the same as indexes in other database systems.
