MongoDB Indexes

MongoDB provides a number of different index types to support specific types of data and queries.
Indexes support the efficient execution of queries in MongoDB.
MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the documents in a MongoDB collection.
MongoDB indexes use a B-tree data structure.

Index Types :

Default _id
Single Field
Compound Index
Multikey Index
Geospatial Index
Text Index
Hashed Index
Default _id – All MongoDB collections have an index on the _id field that exists by default.
If applications do not specify a value for _id, the driver or the mongod will create an _id field with an ObjectId value.
The _id index is unique and prevents clients from inserting two documents with the same value for the _id field.

Single Field – MongoDB supports the creation of user-defined ascending/descending indexes on a single field of a document.
> db.mycol.ensureIndex({“title”:1})

Compound Index – MongoDB supports compound indexes, where a single index structure holds references to multiple fields within a collection’s documents.
> db.products.createIndex( { “item”: 1, “stock”: 1 } )

  • Ask Question