Python MongoDB – drop_index Query

The drop_index() library function in PyMongo is used to drop the index from a collection in the database, as the name suggests. In this article, we are going to discuss how to remove an index from a collection using our python application with PyMongo.

Syntax: drop_index(index_or_name, session=None, **kwargs)
Parameters:

  • index_or_name: The name of the index generated by calling create_index() or ensure_index() method on a collection. If a custom index name was created through the name parameter, then the custom name should be passed here.
  • session: This is an optional argument which specifies the ClientSession(from class pymongo.client_session).
  • kwargs: These are additional keyword arguments(optional).

What Are Indexes?

Indexes are a special data structure used in MongoDB for increasing the efficiency of query execution. They are defined at the collection level and they allow MongoDB to limit the number of documents that it searches. B-tree data structures are used for indexing in MongoDB. There are various types of Indexes such as single-field indexes, compound indexes, multi-key indexes. For the sake of understanding, in this article, we shall use single-field indexes.

On a locally hosted Mongo server, let us create a database test with a collection students. The database will hold the following information about students –



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 students collection, given that the mongo server is running:

Example 1: Adding an Index to the Collection

filter_none

edit
close

play_arrow

link
brightness_4
code

import pprint
import pymongo
  
# connection
try:
    client = pymongo.MongoClient()
    db = client['test']
    print('connection to the server established')
      
except Exception:
    print('Failed to Connect to server')
  
collection = db.students
  
  
# creating an index
resp = collection.create_index("newIndex")
  
# printing the auto generated name 
# returned by MongoDB
print(resp)
  
# index_information() is analogous 
# to getIndexes
pprint.pprint(collection.index_information())

chevron_right


Output:

As we can see the autogenerated name is newIndex_1.

Example 2: Deleting the Index from the Collection

filter_none

edit
close

play_arrow

link
brightness_4
code

import pprint
import pymongo
  
  
try:
    client = pymongo.MongoClient()
    db = client['test']
    print('connection to the server established')
  
except Exception:
    print('Failed to Connect to server')
  
collection = db.students
  
# dropping the index using autogenerated
# name from MongoDB
collection.drop_index("newIndex_1")
  
# printing the indexes present on the collection
pprint.pprint(collection.index_information())

chevron_right


Output:

The output shows that the newly inserted Index called newIndex was dropped and only the original _id index remained. This is the application of drop_index().




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.