Get all the information of a Collection's indexes using PyMongo - GeeksforGeeks

08 Jun, 2020
Prerequisites: MongoDB Python Basics
This article is about displaying the information of Collection’s indexes using the index_information() function of the PyMongo module.
index_information() returns a dictionary where the keys are index names (as returned by create_index()) and the values are dictionaries containing information about each index. The dictionary is guaranteed to contain at least a single key, “key” which is a list of (key, direction) pairs specifying the index (as passed to create_index()). It will also contain any other metadata about the indexes, except for the “ns” and “name” keys, which are cleaned.
Let’s begin:
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 PythonCreating 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_databaseAccessing the Collection: We now select the collection from the database using the following syntax:collection_name = mydatabase.name_of_collectionGetting the information of the indexes: Using the function index_information to get the information of all the indexes of the collection.collection_name.index_information()

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
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)
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
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
collection_name = mydatabase.name_of_collection
Getting the information of the indexes: Using the function index_information to get the information of all the indexes of the collection.collection_name.index_information()

collection_name.index_information()

Example:
Sample Database:
# Python Program for demonstrating the # PyMongo Cursor to Pandas DataFrame     # Importing required modulesfrom pymongo import MongoClientfrom pandas import DataFrame   # Connecting to MongoDB server# client = MongoClient('host_name',# 'port_number')client = MongoClient('localhost', 27017)  # Connecting to the database named# GFGmydatabase = client.GFG  # Accessing the collection named# Studentmycollection = mydatabase.Student   # Displaying the information of all the indexes# using the function index_information()ind_info = mycollection.index_information()print(ind_info)
Output:
{‘_id_’: {‘v’: 2, ‘key’: [(‘_id’, 1)], ‘ns’: ‘GFG.Student’}, ‘Roll No_1’: {‘v’: 2, ‘unique’: True, ‘key’: [(‘Roll No’, 1)], ‘ns’: ‘GFG.Student’}}
Python-mongoDB
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Defaultdict in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()