Prerequisites: MongoDB Python Basics
This article is about rebuilding the indexes of the collection in MongoDB using PyMongo reindex() function.
According to MongoDB documentation:
Normally, MongoDB compacts indexes during routine updates. For most users, the reIndex command is unnecessary. However, it may be worth running if the collection size has changed significantly or if the indexes are consuming a disproportionate amount of disk space.
The collection indexes may take more than disproportionate amount of disk space, and this problem can be solved by rebuilding the index of the collection.
Let’s begin with rebuilding the index of Collection:
- Importing Required Modules: Import the required module using the command:
from pymongo import MongoClient
If MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with Python
- Creating a Connection: Now we had already imported the module, its time to establish a connection to the MongoDB server, presumably which is running on localhost (host name) at port 27017 (port number).
client = MongoClient(‘localhost’, 27017)
- Accessing the Database: Since the connection to the MongoDB server is established. We can now create or use the existing database.
mydatabase = client.name_of_the_database
- Accessing the Collection: We now select the collection from the database using the following syntax:
collection_name = mydatabase.name_of_collection
- Rebuilding Index:
The above statement rebuild all the indexes. Please use the above statement with caution as the rebuilding of indexes takes place in foreground that blocks all the operation on the MongoDB server.
- How to Drop all the indexes in a Collection using PyMongo?
- Get all the information of a Collection's indexes using PyMongo
- Get all the Documents of the Collection using PyMongo
- Update all Documents in a Collection using PyMongo
- What is a PyMongo Cursor?
- Nested Queries in PyMongo
- Get the Names of all Collections using PyMongo
- How to check if the PyMongo Cursor is Empty?
- Convert PyMongo Cursor to JSON
- Convert PyMongo Cursor to Dataframe
- Difference Between insert(), insertOne(), and insertMany() in Pymongo
- Python | Accessing all elements at given list of indexes
- Using Timedelta and Period to create DateTime based indexes in Pandas
- Python | Change column names and row indexes in Pandas DataFrame
- How to Create a Pivot table with multiple indexes from an excel sheet using Pandas in Python?
- Garbage Collection in Python
- How to update data in a Collection using Python?
- How to access a collection in MongoDB using Python?
- Drop Collection if already exists in MongoDB using Python
- How to create index for MongoDB Collection using Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.