How to create index for MongoDB Collection using Python?

Home Forums Python Course Forum Python DB (MySQL) How to create index for MongoDB Collection using Python?

Viewing 0 reply threads
  • Author
    Posts
    • training@h2kinfosys.com
      Keymaster
      (124.123.164.108)

        “Let’s begin with the create_index() method:

        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.
        Syntax:

        create_index(keys, session=None, **kwargs)”

        #18506

    Viewing 0 reply threads
    • You must be logged in to reply to this topic.