Open In App

Index Properties in MongoDB

MongoDB indexes are like the roadmap to our data, guiding our database to quickly find and retrieve information without having to scan every document. They come in various types, each serving a specific purpose to improve performance and efficiency.

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 your database operations.



What is the MongoDB Index?

Types of MongoDB Indexes:

  1. Single Field Index
  2. Compound Index
  3. Multikey Index

  4. Geospatial Index
  5. Text Index

Index Properties

1. 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 Index

2. Partial Indexes

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 Index

Explanation: 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

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 Index

Explanation: The output position_1 indicates that the index on the position field has been successfully created.

4. TTL Indexes

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 Index

Explanation: 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:

Conclusion

MongoDB provides advanced index properties such as unique, partial, sparse, TTL indexes which help to optimize databases performance and, among other things, manage data efficiently. Developers can obtain these index features and apply them to enforce data integrity constraints, optimize query performance, reduce index size, and implement the data expiration policy. Hence, the entire MongoDB database can benefit from the efficiency and salability to end users. Knowing the characteristics of the indices and its consequences is a major requirement towards achieving reasonable and robust database solutions.


Article Tags :