Prerequisites: MongoDB and Python
With the help of drop_indexes() method we can drop all the indexes in a Collection. No parameter is passed in the method. Only default index _id can not be deleted. All the Non _id indexes will be the drop by this method. It means we can only drop the index which we have created.
Sample database used:
By default, each collection has the _id index. All collections compulsorily have at least one index. If all indexes are removed, then a new index will be automatically generated. We can see the indexes present by running the following command:
Now, we can run the following code to add a new Index called newIndex to the collection, given that the mongo server is running:
Example 1: Adding an Index to the Collection
Example 2: Deleting the Index from the Collection
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.