Database
NoSQL
MongoDB
Indexing
Creating Index

Creating Indexes in MongoDB

Indexing is a critical performance optimization technique in MongoDB that significantly improves query speed. By creating indexes, MongoDB can quickly locate and retrieve data, which reduces the time needed for query execution.

Types of Indexes

Single Field Index

A single field index is created on one specific field of a document. It enhances the performance of queries that filter or sort based on this field. This type of index is ideal for simple queries involving a single field.

db.collection.createIndex({ fieldName: 1 });

Compound Index

A compound index includes multiple fields from a document. It is used to optimize queries that involve more than one field. This type of index supports queries that filter or sort based on multiple attributes and can improve performance for complex queries.

db.collection.createIndex({ field1: 1, field2: -1 });

Unique Index

A unique index ensures that all values in the indexed field are unique across all documents in the collection. This type of index is useful for enforcing constraints, such as ensuring that no two documents have the same email address or username.

db.collection.createIndex({ uniqueField: 1 }, { unique: true });

Text Index

A text index allows for full-text search capabilities within string fields. It enables efficient searches for keywords or phrases within text fields, providing features like text scoring and language-specific search.

db.collection.createIndex({ textField: "text" });

Geospatial Index

A geospatial index is designed for querying geographical locations and spatial data. It is essential for applications that involve location-based queries, such as finding nearby points of interest or locations within a specific area.

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

Hashed Index

A hashed index uses a hash of the field’s value to distribute documents evenly across index buckets. This type of index is suitable for equality queries where a uniform distribution of values is desired. However, it is not suitable for range queries or sorting operations.

db.collection.createIndex({ fieldName: "hashed" });

Sparse Index

A sparse index only includes documents where the indexed field exists. This reduces the size of the index and storage requirements, making it useful for fields that are not present in all documents.

db.collection.createIndex({ fieldName: 1 }, { sparse: true });

Partial Index

A partial index indexes only a subset of documents based on a specified filter expression. This type of index reduces the size of the index by including only documents that meet certain criteria or conditions.

db.collection.createIndex({ fieldName: 1 }, { partialFilterExpression: { fieldName: { $exists: true } } });

Managing Indexes

  • List All Indexes: You can view all indexes on a collection to understand what indexes are currently in use.
db.collection.getIndexes();
  • Drop an Index: To remove a specific index, you can drop it by its name.
db.collection.dropIndex("indexName");
  • Drop All Indexes: If needed, you can remove all indexes from a collection.
db.collection.dropIndexes();

Conclusion

Effective use of indexes is crucial for optimizing the performance of queries in MongoDB. By understanding the different types of indexes and how to manage them, you can significantly improve the speed and efficiency of data retrieval operations in your MongoDB database.