MongoDB Indexes Interview Questions

Master MongoDB Indexes with interview-focused questions covering single-field indexes, compound indexes, multikey indexes, text indexes, hashed indexes, TTL indexes, wildcard indexes, geospatial indexes, covered queries, explain plans, and production best practices.

Introduction

Indexes are one of the most important performance optimization techniques in MongoDB.

Without indexes, MongoDB scans every document in a collection.

With indexes, MongoDB directly locates matching documents.

Indexes improve

  • Query Performance
  • Sorting
  • Filtering
  • Aggregation
  • Joins ($lookup)
  • Pagination

However, excessive indexing increases

  • Storage
  • Insert Time
  • Update Time
  • Delete Time

Understanding MongoDB indexes is essential for Backend Developers and Solution Architects.


MongoDB Index Architecture

flowchart LR

Application --> Query --> QueryOptimizer --> MongoIndex --> Documents --> Response

1. What is an Index?

Answer

A MongoDB Index is a data structure that stores field values in sorted order, allowing MongoDB to locate documents quickly.

Without an index

Collection Scan

With an index

Index Lookup


2. Why are indexes needed?

Indexes improve

  • Search Performance
  • Sorting
  • Aggregation
  • Query Execution

Without indexes

1 Million Documents

↓

Read 1 Million Documents

With indexes

1 Million Documents

↓

Read 20 Index Entries

3. What is the default index?

MongoDB automatically creates

_id

index.

Example

{
   "_id":ObjectId(...)
}

4. How do you create an index?

db.employee.createIndex(
{
   employeeId:1
}
)

Ascending

1

Descending

-1

Create Index Flow

flowchart LR

Collection --> createIndex() --> Index --> Query

5. How do you view indexes?

db.employee.getIndexes()

6. How do you drop an index?

db.employee.dropIndex(
"employeeId_1"
)

Drop all indexes

db.employee.dropIndexes()

7. What is a Single Field Index?

Index created on one field.

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

8. What is a Compound Index?

Index on multiple fields.

db.employee.createIndex(
{
 department:1,
 salary:-1
}
)

Compound Index

flowchart LR

Department --> Salary --> Employee

9. What is the Left Prefix Rule?

Compound indexes are used efficiently only when queries begin with the leftmost indexed field.

Example

Index

Department

Salary

Works

{
 department:"IT"
}

Does not efficiently use the index

{
 salary:100000
}

10. What is a Multikey Index?

Automatically created for array fields.

Example

{
 skills:[
   "Java",
   "MongoDB",
   "AWS"
 ]
}

MongoDB indexes each array element.


Multikey Index

flowchart LR

Array --> Java

Array --> MongoDB

Array --> AWS

11. What is a Text Index?

Supports full-text search.

db.books.createIndex(
{
 title:"text"
}
)

Search

db.books.find(
{
 $text:{
   $search:"MongoDB"
 }
}
)

12. What is a Hashed Index?

Stores hash values instead of original values.

Useful for

  • Sharding
  • Equality Queries

Example

db.user.createIndex(
{
 userId:"hashed"
}
)

13. What is a TTL Index?

TTL

Time To Live

Automatically deletes expired documents.

Example

db.logs.createIndex(
{
 createdAt:1
},
{
 expireAfterSeconds:86400
}
)

Deletes logs after one day.


TTL Workflow

flowchart LR

Document --> TTLIndex --> Expiration --> Deletion

14. What is a Wildcard Index?

Indexes every field dynamically.

db.products.createIndex(
{
 "$**":1
}
)

Useful when document fields vary.


15. What is a Sparse Index?

Indexes only documents containing the indexed field.

Example

{
 email:"[email protected]"
}

Documents without email are not indexed.


16. What is a Partial Index?

Indexes only documents matching a filter.

Example

db.employee.createIndex(
{
 salary:1
},
{
 partialFilterExpression:{
   active:true
 }
}
)

17. What is a Unique Index?

Prevents duplicate values.

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

18. What is a Geospatial Index?

Supports location-based queries.

db.location.createIndex(
{
 location:"2dsphere"
}
)

Used for

  • Maps
  • Ride Sharing
  • Food Delivery

19. What is a Covered Query?

A query satisfied completely from the index.

Example

db.employee.find(
{
 email:"[email protected]"
},
{
 email:1
}
)

No document lookup required.


Covered Query

flowchart LR

Query --> Index --> Result

20. What is Index Intersection?

MongoDB can combine multiple indexes for one query.

Example

Department Index

+

Salary Index

Optimizer merges results.


21. What is explain()?

Shows query execution plan.

db.employee.find(
{
 department:"IT"
}
).explain("executionStats")

22. Important explain() stages

  • COLLSCAN
  • IXSCAN
  • FETCH
  • SORT

Goal

Avoid

COLLSCAN

Prefer

IXSCAN

Explain Flow

flowchart LR

Query --> Explain --> IXSCAN

Explain --> COLLSCAN

23. What is COLLSCAN?

Collection Scan.

MongoDB reads every document.

Very slow for large collections.


24. What is IXSCAN?

Index Scan.

MongoDB reads only index entries.

Much faster.


25. Why do indexes slow writes?

Every

  • Insert
  • Update
  • Delete

must also update indexes.


26. Banking Example

Query

db.accounts.find(
{
 accountNumber:1001
}
)

Index

accountNumber

Query time reduced from seconds to milliseconds.


27. E-Commerce Example

Search products

{
 category:"Laptop",
 brand:"Dell"
}

Compound Index

category

brand

28. Social Media Example

Search posts

{
 $text:{
   $search:"MongoDB"
 }
}

Uses

Text Index.


29. Logging Example

Automatically remove logs older than 30 days.

TTL Index

createdAt

No scheduled cleanup required.


30. Geospatial Example

Find restaurants within 5 km.

Uses

2dsphere Index

31. Common indexing mistakes

  • Too many indexes
  • Missing indexes
  • Wrong compound index order
  • Large text indexes everywhere
  • Ignoring explain()
  • Duplicate indexes

Index Optimization Workflow

flowchart LR

SlowQuery --> Explain --> COLLSCAN --> CreateIndex --> IXSCAN --> FastQuery

Enterprise Best Practices

  • Create indexes based on query patterns.
  • Keep compound indexes small.
  • Follow the left-prefix rule.
  • Use text indexes only when required.
  • Use TTL indexes for temporary data.
  • Monitor explain() output regularly.
  • Remove unused indexes.
  • Avoid duplicate indexes.
  • Use covered queries whenever possible.
  • Test index performance using production-sized datasets.

Quick Revision

Topic Key Point
Default Index _id
Single Index One Field
Compound Index Multiple Fields
Multikey Index Arrays
Text Index Full-Text Search
Hashed Index Equality & Sharding
TTL Index Auto Delete
Sparse Index Missing Fields Ignored
Partial Index Filtered Documents
Covered Query Index Only
COLLSCAN Slow
IXSCAN Fast

Interview Tips

Interviewers frequently ask

  • What is an index?
  • Explain Compound Index.
  • What is the Left Prefix Rule?
  • Explain Multikey Index.
  • What is a TTL Index?
  • Difference between Sparse and Partial Index.
  • What is a Covered Query?
  • Explain COLLSCAN vs IXSCAN.
  • What does explain() show?
  • Give a production indexing example.

Always explain that indexes dramatically improve read performance but increase write overhead and storage usage. Mention that **explain() is the primary tool used to verify whether MongoDB is actually using an index in production.


Summary

MongoDB indexes are essential for building high-performance applications. They enable efficient searching, sorting, aggregation, and querying while reducing collection scans and improving response times. MongoDB supports a rich set of index types—including single-field, compound, multikey, text, hashed, TTL, wildcard, sparse, partial, and geospatial indexes—to optimize different workloads.

Understanding index types, the left-prefix rule, covered queries, execution plans, and index maintenance is critical for designing scalable MongoDB applications and succeeding in backend engineering, cloud, and solution architect interviews.