Video Transcript (English)
Welcome! In this video, you will learn how to install PyMongo, the official MongoDB driver for synchronous Python applications, by using pip, the package installer for Python.
You’ll also learn how to connect a MongoClient instance in a Python application to an Atlas cluster by using a connection string.
Finally, you’ll learn the importance of using a single MongoClient that can be reused across all database requests.
Let’s get started by using pip to install PyMongo.
First, create a directory for your project. Then, in this directory, create and activate a Python virtual environment. Once you’ve done so, you’re ready to install PyMongo.
First open the project directory in your favorite code editor. We’ll use VS Code for this demo.
If you're using a different IDE, or if you’re using a text editor, the experience will be slightly different, but the code will be the same.
We’ve created a directory named `python-test`, and we have our virtual environment set up and activated.
Next, let’s install PyMongo. Along with the driver, we’ll also install `dnspython`. This is a third-party toolkit that’s required to support the connection string format used by Atlas.
To install PyMongo and `dnspython` by using pip, run pip install in the terminal.
Now that the driver is installed, let’s retrieve our connection string so that we can test connecting to our Atlas cluster by using PyMongo.
First, we log into Atlas.
We ensure we’re on the Database tab of the project dashboard.
We see that there’s one cluster in this project, named Cluster0. We click the Connect button.
This opens a new window, which lists the connection options: Connect with the MongoDB Shell, connect your application, connect using MongoDB Compass, and connect using VS Code.
We want to connect our application, so we select that option.
This brings up another window where we need to select the Python driver.
We need to choose the PyMongo version that we want to use. For this example, we’ll use the latest version available.
We also have an option to include the driver import statements if needed. For this demo, we’ll only select the connection string, which is also known as the connection URI.
Copy the connection string by selecting the copy icon.
Notice that the password value is not included in the connection string we just copied. We’ll need to replace the placeholder with its value in our Python file.
Now that we’ve retrieved our connection string, we can use it to connect our application to our Atlas cluster by using PyMongo.
Create a new file called `connection.py` at the root of the project.
Open `connection.py` in an editor tab.
First, import the MongoClient class from PyMongo. MongoClient is used to initiate the connection with the database.
Then assign the connection string to a variable. Notice that we need to enclose the connection string in parentheses.
For simplicity, in this demo we’re inserting the connection string directly. But in future projects, you’ll want to load the connection string from a secure location by using an approach that suits your needs.
Don’t forget to replace the password placeholder with its value. If your database has more than one user, you’ll need to add the username value, too. The cluster name will already be included.
Then create a MongoClient instance, passing in the connection string as an argument.
With these few lines of code, we now have access to our Atlas cluster via the MongoClient.
We can test the connection by looping through the databases in our cluster and printing out their names.
Let’s save and run this script. The names of the Atlas training databases loaded on this cluster should print out.
Excellent! Now we’re connected, and the names of all the databases on the cluster printed to the terminal.
Note that an application should use a single MongoClient instance for the life of the application, to be reused across all database requests. This is because creating MongoClients is resource intensive.
Creating a new MongoClient for each database request will degrade the application’s performance.
Congratulations! Now you know how to connect to an Atlas cluster from your Python application by using PyMongo.
Here are some key takeaways from this video:
Use pip to install PyMongo.
We need a valid connection string to connect our Python application to the database via the driver.
The connection string is available in the Atlas dashboard.
And an application must use a single MongoClient instance for the life of the application, to be reused across all database requests.