</>
Skip to content
MongoDB lessons (25/38)

MongoDB — Indexes

Create Index

db.users.createIndex({ email: 1 })

Unique Index

db.users.createIndex({ email: 1 }, { unique: true })

Compound Index

db.users.createIndex({ country: 1, age: -1 })

List Indexes

db.users.getIndexes()

Drop Index

db.users.dropIndex({ email: 1 })

Index Types

TypeDescription
SingleOne field
CompoundMultiple fields
UniqueNo duplicates
TextFull-text search
GeospatialLocation data
TTLAuto-expire

Index Properties

PropertyDescription
uniqueNo duplicate values
sparseSkip null values
expireAfterSecondsTTL index

Mini Practice

  1. Create basic index
  2. Create unique index
  3. Create compound index
  4. Check index usage

Up Next

Continue with Text Search — full-text search.

Related Topics

Frequently Asked Questions about Indexes

What is Indexes in MongoDB?

Indexes is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Indexes?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Indexes.

Why is Indexes important in MongoDB?

Indexes is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.