Skip to main content

What are indexes in MongoDB?

Updated 1 min readMarkdown
Part of seriesMongoDB
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> )

Sources:

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

About the author

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

No comments yet. Be the first to comment.

More from this blog