Indexing in MongoDB is used for improved query performance. Indexing in MongoDB can have following advantages.
- Speed-Up Queries
- Reduce Disk I/O
- Reduces Resource Utilization
Why Indexes Required:
MongoDB’s indexes enable the database to locate documents without scanning each record, significantly improving query performance.Indexes reduce CPU and memory consumption during query execution.Fields like login and email can be made unique by indexes.
Types of Indexes in MongoDB:
- Single-Field Index.
- Compound Index
- Multi-key Index
- Text Index
- Wild-card index
Single-Field Index:
The simplest kind of index in MongoDB is a single field index. It improves the speed of queries that filter or sort by a single field in a document by creating an index on that field.MongoDB can navigate in both ascending and descending order using a single field index. Because of this, the index key is irrelevant in this instance.
They are especially advantageous for tasks such as filtering, sorting, or searching data based on a single attribute.
Syntax: db.collection.createIndex({ fieldName: 1 })

Compound Index:
An index with many fields is called a compound index. It can enhance the efficiency of queries with several fields, particularly when filters, sorts, or projections employ both fields.A compound index’s field order is crucial since it directly influences how well it responds to particular query patterns.

We have set of Documents in the users collections. In this I have created Compound Index for Age and City Column.
Syntax: db.collection.createIndex({ field1: 1, field2: -1 })

Multi-Key Index:
An index that enables array indexing is called a multi-key index. Whenever you construct an index on a field that contains an array, MongoDB automatically generates a multi-key index.In order to facilitate effective searching and filtering based on these nested values, a multikey index creates distinct index entries for every value within an array or nested document.
Syntax: db.collection.createIndex({ fieldName: 1 })

Text Index:
A text index enables full-text search on string content in fields like names, titles, descriptions, or article bodies.When text-based analysis and natural language processing are crucial, text indexes are especially helpful.
Syntax:db.collection.createIndex({ field: “text” })

Wild-card Index:
In MongoDB, the wildcard index { “$**”: 1 } creates an index on all fields, including nested fields, in each document in a collection.
Syntax:db.collection.createIndex({ “$**”: 1 })

Monitoring Index Usage:
It’s fantastic to create indexes, but are your queries really using them? MongoDB has robust tools to track index utilization, which can help you optimize performance and eliminate unnecessary overhead. Here are some of steps in Monitoring Index usage.
Check Existing Indexes:
To list all the current Indexes I used db.users.getIndexes() command. This will list about all the available Indexes.

Analyze Query:
The MongoDB’s .explain() method reveals how your queries are executed.

Compatibility changes for indexing in MongoDB 8.0:
1) Query Behaviour
Previous behavior (before 8.0)
When searching for documents with { field: null }, MongoDB matches documents where the field is either:
Set explicitly to null or Missing (the field was undefined).
In 8.0
The { field: null } query only matches documents with the field explicitly set to null as of version 8.0.Documents with missing (undefined) fields are no longer matched.

Reason for the changes:
- MongoDB now aligns to more accurate type semantics thanks to the new behavior.
- This enhances the clarity of data modeling and prevents accidental matches.
- Additionally, it facilitates more uniform behavior when executing queries across drivers and APIs.
2) Improved Indexing Systems:
The introduction of advanced indexing algorithms in MongoDB 8.0 improves query response times, particularly for complex queries with numerous fields. MongoDB now improves compound indexes and wildcard indexes.
Wildcard Indexes:
MongoDB 8.0 supports wildcard indexing of dynamic or unknown fields ahead of time. This is especially beneficial for unstructured data, because the field names may not be known in advance. Wildcard indexes allow you to create indexes for every field in a document, boosting query efficiency without having to manually create indexes for each available field.
Compound Index:
MongoDB 8.0 optimizes compound indexes by allowing more complicated index type combinations, which improves query time when filtering across several fields.
3) Enhanced Query Planning and Execution
MongoDB 8.0 significantly improves the explain() method, giving users more insight into query planning and execution. Here’s a summary of the significant improvements.
The Explain() in MongoDB 8.0 includes:
rejectedPlans: An array of query plans examined but not selected by the optimizer. This enables engineers to understand why specific plans were abandoned.
optimizationTimeMillis: The number of milliseconds spent by the query planner optimizing the query. This statistic is used to analyze the planning phase’s efficiency.
planCacheShapeHash and planCacheKey are hash values that reflect the query shape and the corresponding plan cache entry, respectively. These are helpful in discovering and managing cached query plans.
4) Performance Improvements in MongoDB 8.0:
Read Throughput: Increased read throughput by 36%, which improved data retrieval speeds.
Bulk Writes: Write operations are now 56% faster, increasing data insertion efficiency.
5) Aggregation Pipeline Optimization:
Although MongoDB’s aggregation mechanism is sophisticated, it might suffer from performance deterioration when dealing with complex queries or huge datasets. The aggregation pipeline in MongoDB 8.0 has been optimized to boost throughput and reduce latency. One of the most significant enhancements is the handling of large-scale joins and group activities.
- More effective join algorithms are used for faster joins.
- Reduced scans: Lowers the number of full collection scans.
- Sending additional actions to the target collection is known as pushdown capability.
- Reduced in-memory data buffering results in lower memory utilization.
- Improved performance: Notable increases in speed for joining big datasets.
Conlcusion:
Significant improvements to indexing and query efficiency are brought about by MongoDB 8.0, including improved aggregation capabilities, sophisticated indexing algorithms, and the Express Path optimization. Because of these enhancements, it is a strong option for applications that need effective data processing and retrieval.
References:
https://www.mongodb.com/blog/post/mongodb-8-0-raising-the-bar
https://www.todosdb.com/blog/mongodb-8-0-enhancements-in-depth-overview-of-technical-advancements
Trackbacks/Pingbacks