Importing PyMongo Module: Import the PyMongo module using the command:
from pymongo import MongoClient
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
Creating a index: Now we will create the index using create_index() function.