Create a Connection String
You can connect to your MongoDB deployment by providing a connection URI, also called a connection string, which instructs the driver on how to connect to a MongoDB deployment and how to behave while connected.
The connection string includes the hostname or IP address and port of your deployment, the authentication mechanism, user credentials when applicable, and connection options.
To connect to an instance or deployment not hosted on Atlas, see Choose a Connection Target in the PyMongo documentation.
Find your MongoDB Atlas connection string
To retrieve your connection string for the deployment that you created in the previous step, log into your Atlas account and navigate to the Clusters section and click the Connect button for your new deployment.

Proceed to the Connect your application section and select "Python" from the Driver selection menu and the version that best matches the version you installed from the Version selection menu.
Edit and save the connection string
Paste your connection string into a file in your preferred text editor and save this file to a safe location for use in the next step. Your connection string resembles the following example:
mongodb+srv://<username>:<password>@samplecluster.jkiff1s.mongodb.net/?retryWrites=true&w=majority&appName=SampleCluster
Replace the <username>
and <password>
placeholders with
your database user's username and password.
Then, specify a connection to the sample_mflix
database from the
Atlas sample datasets by adding it after the hostname, as shown in
the following example:
mongodb+srv://<username>:<password>@samplecluster.jkiff1s.mongodb.net/sample_mflix?retryWrites=true&w=majority&appName=SampleCluster
After completing these steps, you have a connection string that contains your database username, database password, and database name.