The Wayback Machine - https://web.archive.org/web/20250129001431/https://www.geeksforgeeks.org/indexing-in-mongodb/
Open In App

Indexing in MongoDB

Last Updated : 08 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Indexing in MongoDB is a crucial feature that enhances query processing efficiency. Without indexing, MongoDB must scan every document in a collection to retrieve the matching documents and leading to slower query performance. Indexes are special data structures that store information about the documents in a way that makes it easier for MongoDB to quickly locate the right data.

In this article, We will learn about Indexing in MongoDB by understanding how to create, drop and get an Index with the help of examples and so on.

Indexing in MongoDB 

  • MongoDB uses indexing to make the query processing more efficient.
  • If there is no indexing, then MongoDB must scan every document in the collection and retrieve only those documents that match the query.
  • Indexes are special data structures that store some information related to the documents such that it becomes easy for MongoDB to find the right data file.
  • The indexes are ordered by the value of the field specified in the index. 

Creating an Index

MongoDB provides a method called createIndex() that allows users to create an index. 

Syntax: 

db.COLLECTION_NAME.createIndex({KEY:1}) 

The key determines the field on the basis of which we want to create an index and 1 (or -1) determines the order in which these indexes will be arranged(ascending or descending). 

Example of Indexing in MongoDB

db.mycol.createIndex({“age”:1})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}

The createIndex() method also has a number of optional parameters. 
These include:  

  • background (Boolean) 
  • unique (Boolean) 
  • name (string) 
  • sparse (Boolean) 
  • expireAfterSeconds (integer)
  • hidden (Boolean) 
  • storageEngine (Document)

Drop an index 

In order to drop an index, MongoDB provides the dropIndex() method. 

Syntax:

db.NAME_OF_COLLECTION.dropIndex({KEY:1}) 

The dropIndex() methods can only delete one index at a time. In order to delete (or drop) multiple indexes from the collection, MongoDB provides the dropIndexes() method that takes multiple indexes as its parameters. 

Syntax: 

db.NAME_OF_COLLECTION.dropIndexes({KEY1:1, KEY2: 1}) 

The dropIndex() methods can only delete one index at a time. In order to delete (or drop) multiple indexes from the collection, MongoDB provides the dropIndexes() method that takes multiple indexes as its parameters. 

Get Description of All Indexes

The getIndexes() method in MongoDB gives a description of all the indexes that exists in the given collection. 

Syntax  

db.NAME_OF_COLLECTION.getIndexes() 

It will retrieve all the description of the indexes created within the collection.

FAQs on MongoDB Index

What is an index in MongoDB?

An index in MongoDB is a special data structure that stores a portion of the data set in an easy-to-traverse form. Indexes are used to efficiently query and retrieve documents from a collection by providing quick access to the data based on the indexed fields.

How many indexes are in MongoDB?

By default, MongoDB supports up to 64 indexes per collection. This includes the default index on the _id field that MongoDB creates automatically for every collection.

Can MongoDB use multiple indexes?

Yes, MongoDB can use multiple indexes to optimize query performance. When a query involves multiple fields, MongoDB’s query optimizer can evaluate multiple indexes to determine the most efficient way to execute the query. Additionally, compound indexes, which index multiple fields within a single index, can be created to further enhance query efficiency.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg