Skip to main content

Command Palette

Search for a command to run...

What are indexes in MongoDB?

Updated
1 min read
What are indexes in MongoDB?
R

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference

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> )

Sources:

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

More from this blog

R

Raja Muhammad Asher - Senior Software Engineer - Full Stack Developer

157 posts

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, ExpressJS, MySQL, MongoDB. Love collaborating & making a difference