Index | How To Show And Drop An Index In MongoDB

In this tutorial, we will learn How to show and drop an index in MongoDB as to show and drop the index we need to use the collection’s get Index() method and for dropping it we need to use the drop Index() method and here we will discuss the examples of each with code.

Index

Index

Asit is a data structure that is used after the speed of data retrieval from collections, It also stores the subset of the data in an efficient manner. which makes it quick to get the data.

As it is the same index as in any other database which specifies the location of collections where they are stored and we can easily get that collection if we have the index as the data is arranged as per the indexes in a particular order.

There are different types of indexes that we could use in our database which are as followed.

  1. Single Field: created for a specific or single field.
  2. Compound Indexes: It is used for multiple fields in a collection.
  3. MultiKey indexes: They are used for making the mao on an array or we can say they have used an array.
  4. Geospatial Index: It is also used only for Geospatial fields only.

Now we have to show and drop the indexes for that we have different methods SO we will see the code of the example here.

For getting the indexes

db. collection. getIndexes()

it will list all the indexes that are present in the collections.

For dropping the indexes the method which could be used for dropping either only one or all the indexes at one time only means we can drop all the indexes in one go and we can also choose to drop them one by one by naming them as per our choice.

Let’s take the example of both one by one

db. collection. dropIndex({ index_ name: 1})

It will drop only one index as we have given the name of the index to drop that is given in the argument of the drop index method so it is for only one index to drop.

For dropping all the indexes present in the collection follow the code given below.

db. collection. dropIndexes()

It will drop all the indexes which are present in the collection.

 

To learn more about How To Show And Drop An Index In MongoDB and more about indexing visit: Indexing in MongoDB Collections.

To learn more about python visit: Python Tutorials And Problems.

 

Leave a Comment

%d bloggers like this: