Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Drop all the indexes in a Collection using PyMongo?
Next article icon

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:

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 databasetest with a collectionstudents

The database will hold the following information about students as follows: 

 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 

Python3
importpprintimportpymongo# connectiontry:client=pymongo.MongoClient()db=client['test']print('connection to the server established')exceptException:print('Failed to Connect to server')collection=db.students# creating an indexresp=collection.create_index("newIndex")# printing the auto generated name# returned by MongoDBprint(resp)# index_information() is analogous# to getIndexespprint.pprint(collection.index_information())

Output: As we can see the autogenerated name is newIndex_1.Example 2:Deleting the Index from the Collection 

Python3
importpprintimportpymongotry:client=pymongo.MongoClient()db=client['test']print('connection to the server established')exceptException:print('Failed to Connect to server')collection=db.students# dropping the index using autogenerated# name from MongoDBcollection.drop_index("newIndex_1")# printing the indexes present on the collectionpprint.pprint(collection.index_information())

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().

It throws OperationFailure error when trying to drop an index that does not exist.


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp