
What Are MongoDB Indexes?
MongoDB indexes are special data structures that
improve query performance by allowing MongoDB to
quickly locate specific documents without scanning the
entire collection.
Imagine a large library:
* Without an index, you must check every book one by
one.
* With an alphabetical index, you can directly jump to
the correct section.
MongoDB indexes work exactly the same way.
By default, MongoDB automatically creates an index
on the _id field, but developers can also create custom
indexes on other fields to optimize database searches.
⸻⸻⸻
Why Are Indexes Important in MongoDB?
Without indexes:
* MongoDB scans every document in the collection.
* Queries become slower as the database grows.
With indexes:
* MongoDB searches only the indexed data.
* Query execution becomes much faster.
* Large-scale applications perform better.
Indexes are extremely important in:
* AI applications
* Mobile applications
* Backend systems
* Real-time systems
* Large production databases
⸻⸻⸻
Sample MongoDB Employee Collection
Example document:
{
name: “John”,
department: “IT”,
salary: 5000
}
⸻⸻⸻
1. MongoDB find() Command
Purpose
The find() command retrieves documents from a
MongoDB collection.
Syntax
db.employees.find()
Example
db.employees.find({ department: “IT” })
What It Does
This query returns all employees whose department is “IT”.
Problem Without Index
MongoDB may scan the entire collection to find
matching documents.
This process is called:
* Collection Scan (COLLSCAN)
On very large collections, this becomes slow.
⸻⸻⸻
2. MongoDB explain(“executionStats”)
Purpose
The explain() method shows how MongoDB executes
a query and helps analyze performance.
Syntax
db.employees.find({ department: “IT”
}).explain(“executionStats”)
Example Output
executionStats:
{
nReturned: 3,
totalDocsExamined: 6
}
Explanation
* nReturned
* Number of matching documents returned.
* totalDocsExamined
* Total documents MongoDB checked during the search.
In this example:
* MongoDB returned 3 documents.
* But it examined all 6 documents.
This is inefficient for large databases.
⸻⸻⸻
3. MongoDB createIndex()
Purpose
The createIndex() command creates an index on a field.
Indexes dramatically improve search performance.
Syntax
db.collection.createIndex({ field: 1 })
Example
db.employees.createIndex({ department: 1 })
Explanation
* department
* Field to index.
* 1
* Ascending order.
* -1
* Descending order.
MongoDB creates an index named:
department_1
⸻
How MongoDB Indexes Improve Performance
After creating the index:
db.employees.find({ department: “IT” }).explain(“executionStats”)
Example result:
executionStats:
{
nReturned: 3,
totalDocsExamined: 3
}
Improvement
Before index:
* Examined 6 documents.
After index:
* Examined only 3 documents.
MongoDB now searches through the index first instead
of scanning the entire collection.
⸻⸻⸻
4. MongoDB getIndexes()
Purpose
Displays all indexes created in a collection.
Syntax
db.collection.getIndexes()
Example
db.employees.getIndexes()
Example Output
[
{ key: { _id: 1 }, name: “_id_” },
{ key: { department: 1 }, name: “department_1” }
]
Explanation
* _id_
* Default MongoDB index.
* department_1
* Custom index created on the department field.
⸻⸻⸻
5. MongoDB dropIndex()
Purpose
Deletes an existing index.
Syntax
db.collection.dropIndex(“index_name”)
Example
db.employees.dropIndex(“department_1”)
What It Does
Removes the custom index from the collection.
After deletion:
db.employees.getIndexes()
Only the default _id index remains.
⸻⸻⸻
Advantages of MongoDB Indexes
* Faster query execution
* Better application performance
* Reduced collection scanning
* Improved scalability
* Faster filtering and sorting
⸻
Disadvantages of MongoDB Indexes
* Consume additional storage
* Increase RAM usage
* Slow down insert and update operations
* Too many indexes reduce performance
* Require maintenance in large databases
⸻⸻⸻
Best Practices for MongoDB Indexing
* Create indexes only on frequently queried fields.
* Avoid unnecessary indexes.
* Use compound indexes carefully.
* Monitor performance using explain().
* Remove unused indexes regularly.
⸻⸻⸻
Conclusion
MongoDB indexes are essential for building fast and scalable applications. Without indexes, MongoDB scans entire collections, which becomes slow for large datasets. By creating indexes on frequently searched fields, developers can dramatically improve database performance and reduce query execution time.
Understanding commands like:
* find()
* explain()
* createIndex()
* getIndexes()
* dropIndex()
is fundamental for every MongoDB developer.
