Mongo Indexes should be known

JackyNote ⭐️
2 min readSep 12, 2023

When working with MongoDB, understanding and properly utilizing indexes is essential for optimizing query performance. MongoDB provides various types of indexes to support different query patterns and requirements. Here are some important types of indexes you should be aware of:

1/ Single Field Indexes

These are the most basic indexes and are created on a single field. They are effective for queries that filter documents based on one field. For example:

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

The 1 indicates an ascending index; you can use -1 for descending.

2/ Compound Indexes

These indexes involve multiple fields and are used when queries filter on multiple fields simultaneously. A compound index can improve query performance for compound queries:

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

In this example, both field1 and field2 are included in the index.

3/ Text Indexes

Text indexes are used for full-text search. They enable efficient text-based searches on string fields:

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

4/ Geospatial Indexes

Geospatial indexes support queries involving geographic data, such as location-based queries:

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

The 2dsphere index is used for storing GeoJSON data.

5/ Hashed Indexes

Hashed indexes are useful for sharding and for distributing data uniformly across shards. They are created by hashing a field’s value:

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

6/ Unique Indexes

Unique indexes enforce uniqueness constraints on a field. You cannot insert two documents with the same indexed field value:

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

7/ Partial Indexes

Partial indexes are used to index a subset of documents in a collection based on a specified filter condition. They can be valuable for optimizing queries with specific criteria:

db.collection.createIndex(   { indexed_field: 1 },   { partialFilterExpression: { status: "active" } } );

8/ Wildcard Indexes

Introduced in MongoDB 5.0, wildcard indexes allow indexing all fields in a document without specifying them explicitly:

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

This can be useful for queries that involve dynamic or unknown fields.

9/ Collation Indexes

Collation indexes support case-insensitive and accent-insensitive string searches. They enable locale-specific sorting and comparison:

db.collection.createIndex({ text_field: 1 }, { collation: { locale: "en", strength: 2 } });

10/ Time-To-Live (TTL) Indexes

TTL indexes automatically remove documents from a collection after a specified time interval. They are used for data expiration:

db.collection.createIndex({ expire_at: 1 }, { expireAfterSeconds: 3600 });

These are some of the most commonly used index types in MongoDB. Choosing the right index type and defining indexes based on your application’s query patterns is crucial for achieving optimal query performance and efficient data retrieval. Properly designed and maintained indexes can significantly speed up your MongoDB queries.



JackyNote ⭐️

🚀 Software Engineer | Full Stack Java 7 Years of Experience | Tech Enthusiast | Startup Lover | Founder of - URL Shorten | Coffee