What are indexes in MongoDB?

Indexes in MongoDB are similar to indexes in other database systems. Indexes support the efficient execution of queries in MongoDB. Indexes are special B-tree data structures that store a small portion of the collection's data set in an easy to traverse form. MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the documents in a MongoDB collection.

MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.

To create an index in the Mongo Shell, use db.collection.createIndex()

db.collection.createIndex( <key and index type specification>, <options> )


Indexes — MongoDB Manual. mongodb.com/docs/manual/indexes.