codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

20 / 65 topics
19Indexing Basics20Creating Indexes21Compound Indexes22Unique Indexes23Geospatial Indexes24Explain Command
Tutorials/MongoDB/Creating Indexes
🍃MongoDB

Creating Indexes

Updated 2026-04-20
3 min read

Creating Indexes

Introduction

Indexes are essential for optimizing query performance in MongoDB. They allow MongoDB to quickly locate and retrieve data without scanning every document in a collection. This tutorial will cover the basics of creating indexes, including single-field, compound, multi-key, text, and geospatial indexes. We'll also discuss best practices for index management.

Prerequisites

Before diving into indexing, ensure you have:

  • A running MongoDB instance.
  • Basic knowledge of MongoDB shell or a MongoDB client like Compass.

Types of Indexes

1. Single-Field Indexes

Single-field indexes are the simplest type of index. They improve query performance for queries that filter documents based on a single field.

Creating a Single-Field Index

To create a single-field index, use the createIndex method:

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

The 1 indicates ascending order; use -1 for descending order.

Example:

Create an index on the name field in ascending order:

db.users.createIndex({ name: 1 });

2. Compound Indexes

Compound indexes improve query performance for queries that filter documents based on multiple fields, especially when those fields are specified in the same order as the index.

Creating a Compound Index

To create a compound index, specify an array of field names:

db.collection.createIndex({ fieldName1: 1, fieldName2: -1 });

Example:

Create a compound index on name and age, with name in ascending order and age in descending order:

db.users.createIndex({ name: 1, age: -1 });

3. Multi-Key Indexes

Multi-key indexes are used for arrays. They index each element of an array separately.

Creating a Multi-Key Index

To create a multi-key index on an array field, use the createIndex method:

db.collection.createIndex({ "arrayField.$": 1 });

Example:

Create a multi-key index on the tags array field:

db.products.createIndex({ "tags.$": 1 });

4. Text Indexes

Text indexes are used for full-text search queries.

Creating a Text Index

To create a text index, use the createIndex method with the text type:

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

Example:

Create a text index on the description field:

db.products.createIndex({ description: "text" });

5. Geospatial Indexes

Geospatial indexes are used for geospatial queries.

Creating a Geospatial Index

To create a geospatial index, use the createIndex method with the appropriate type:

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

Example:

Create a 2dsphere index on the location field for geospatial queries:

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

Best Practices

1. Index Only Necessary Fields

Creating too many indexes can slow down write operations and increase storage requirements. Index only the fields that are frequently queried.

2. Use Compound Indexes Wisely

Compound indexes can significantly improve performance for specific queries, but they must be used judiciously to avoid unnecessary overhead.

3. Monitor Index Usage

Use MongoDB's explain method to analyze query performance and determine if indexes are being utilized effectively:

db.collection.find({ fieldName: value }).explain("executionStats");

4. Regularly Maintain Indexes

Remove unused or redundant indexes to optimize performance. Use the dropIndexes method to remove an index:

db.collection.dropIndex(indexName);

5. Consider Index Prefixing

For compound indexes, consider prefixing with fields that are most frequently used in queries.

Conclusion

Indexes are a powerful tool for optimizing MongoDB query performance. By understanding the different types of indexes and best practices for their use, you can significantly improve the efficiency of your database operations. Always monitor and maintain your indexes to ensure optimal performance.


PreviousIndexing BasicsNext Compound Indexes

Recommended Gear

Indexing BasicsCompound Indexes