Index Properties in MongoDB
Last Updated :
20 Feb, 2025
Indexes in MongoDB play a crucial role in enhancing the performance of database operations. By creating indexes on specific fields, MongoDB can quickly locate data without scanning every document in a collection and speeding up query execution.
In this article, We will learn about the MongoDB Index and its various Index Properties with the help of examples to discover how they can optimize our database operations.
What is the MongoDB Index?
In MongoDB, indexes are data structures that store a small portion of the collection's data in an easy-to-traverse form. They make the database perform rapid searches by using indexed fields rather than downloading whole documents. Indices resolve issues on complex queries more efficiently.
MongoDB has different types of indexing mechanisms like single field indexes, compound indexes, multikey indexes, geospatial indexes, and more. Indexes perform specific tasks for creating databases that meet the performance needs of developers' applications to their specific departments.
Types of MongoDB Indexes
- Single Field Index
- Compound Index
- Multikey Index
- Geospatial Index
- Text Index
Index Properties1. Unique Indexes
Unique indexes ensure that the fields have unique values within the collection for all the documents. Such feature guarantees the unique value per each one of the collection's documents. Entering or altering a document with duplicate value will result in an error. Unique indexes can be used for data integrity constraints like ensuring that the information is unique and for preventing duplicated entries.
To make an index unique, we can use the db.collection.createIndex() method and set the unique option to true.
db.collection.createIndex({ "fieldName": 1 }, { unique: true })
Example:
Let's create a MongoDB collection named employees and ensure that the name field is unique for each document by creating a unique index:
Create a unique index on the "name" field
db.employees.createIndex({ "name": 1 }, { unique: true })
Now, let's insert a document into the employees collection:
db.employees.insertOne({ "name": "John Doe", "position": "Manager", "department": "Sales" })
Output:
Unique Index2. Partial Indexes
Partial indexes focus on indexing only a subset of documents in a collection, specifically those that meet a predefined filter expression. By indexing a smaller set of documents, partial indexes reduce the overall index size compared to indexing the entire collection. This can lead to more efficient use of storage and memory.
Partial indexes are useful when we want to index data based on certain conditions or criteria, allowing for more targeted and efficient indexing strategies.
To create a partial index, use the db.collection.createIndex() method with the partialFilterExpression option.
db.collection.createIndex({ "fieldName": 1 }, { partialFilterExpression: { "status": "active" } })
Example:
Suppose we want to create a partial index on the department field for documents where the position field is set to "Manager". Let's create this partial index. Create a partial index on the "department" field for documents with "position" set to "Manager".
db.employees.createIndex(
{ "department": 1 },
{ partialFilterExpression: { "position": "Manager" } }
Output:
Partial IndexExplanation: The output department_1 indicates that the partial index on the department field has been successfully created with the index key department_1.
3. Sparse Indexes
Sparsity indexing indexes only documents that have the indexed field, excluding those that do not have it. It is beneficial when indexing fields that are missing or sparse in most documents. Sparse indexes help optimize index size and query performance by indexing only the documents containing the indexed element.
To create a sparse index, use the db.collection.createIndex() method with the sparse option set to true.
db.collection.createIndex({ "fieldName": 1 }, { sparse: true })
Example:
Suppose we want to create a sparse index on the position field to include only documents that have the position field populated. Let's create this sparse index. Create a sparse index on the "position" field.
db.employees.createIndex({ "position": 1 }, { sparse: true })
Output:
Sparse IndexExplanation: The output position_1 indicates that the index on the position field has been successfully created.
4. TTL Indexes
TTL indexes are special indexes in MongoDB used for automatic removal of documents from a collection after a specified time. They are ideal for implementing data expiration policies, like clearing temporary or cached data. Common use cases include managing time-sensitive data such as session information or log entries.
To set up a TTL index, apply the createIndex() method to a field that holds either a date or an array of date values. Specify the expireAfterSeconds option with your chosen TTL value in seconds.
db.collection.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 })
Example:
Let's create a TTL index on the createdAt field to automatically delete documents after 24 hours:
db.employees.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 86400 })
Output:
TTL IndexExplanation: The output createdAt_1 indicates that the index on the createdAt field has been successfully created.
Best Practices for Index Utilization
To leverage index properties effectively and optimize database performance in MongoDB, developers should adhere to best practices:
- Analyze Query Patterns: Understand the application querying patterns and focus on the frequently executed queries that will better benefit from indexing.
- Create Indexes Strategically: Create indexes that match query patterns. It should be you’re prioritizing fields involved in filter condition, sorting and aggregation.
- Monitor Index Usage: Watch usage of indexes and to check if they are well performing, know bottlenecks and areas of optimization.
- Avoid Over-Indexing: On the one hand, indexing enhances speed of queries, while on the other, too many indexes increase storage overhead and make write operations slower. It is recommended to index selectively basing on actual usage profiles to prevent information overload.
- Regular Maintenance: Indexes need to be reviewed and amended from time to time depending on application requirements evolution and information governance strategy.
Conclusion
In conclusion, understanding index properties in MongoDB is crucial for optimizing performance and ensuring efficient data retrieval. Different types of indexes, such as single field, compound, text, and geospatial indexes, cater to various query needs and improve the speed of searches and operations. By carefully selecting the appropriate index type and considering factors like query patterns and storage requirements, developers can enhance the scalability and responsiveness of their MongoDB databases.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
5 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read