Python project with MongoDB
The first thing to do when using MongoDB with Python is to install the required library.
Installing the required library
If you are using PyCharm, as I recommend, the only thing you have to do is create a blank project, and then inside the project create a file called requirements.txt.
Then, inside this file, write the name of the library that you want to install. In our case, we want to install the library pymongo.
Optionally, we can also specify (as we did in the last section) the version number of the library. Type the following into your requirements file:
pymongo==3.2.2
If you omit ==3.2.2, then the latest version will be installed, although this is not recommended.
Always installing the latest version can seem like a good idea. However, if you create your application using v3.2.2, and then an update to the library comes along which you don't investigate, the update might break your application.
Always specify a version number in your requirements file.
When you go into a Python file to start the project, PyCharm will prompt you to install the libraries that you have not already installed from your requirements file.
Next up, lets look at list comprehension in Python. It's a small aside from the project which will nevertheless be extremely useful!
Using the pymongo library
First, create a file called app.py in your project. We are going to write a small program that will help us understand how to interact with MongoDB in our Python programs.
Since we are using the library pymongo and it is not in the same file that we are writing, we have to import it. After importing, your file will look something like this:
import pymongo
__author__ = "Your Name"
Every mongod process has associated with it a Universal Resource Identifier. This identifier allows a program to find and connect with the process. The URI is often this:
mongodb://127.0.0.1:27017
If you are using Docker to run MongoDB, your URI will look different. You should be able to see your URI in the Kinematic window.
Lets add our URI to our program:
import pymongo
__author__ = "Your Name"
mongodb_uri = "mongodb://127.0.0.1:27017"
The pymongo library allows our program to create a variable of type MongoClient, which is what gives us the functionality to interact with MongoDB. This variable needs the URI we have created, so it knows where the mongod process lives.
import pymongo
__author__ = "Your Name"
mongodb_uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(mongodb_uri)
Next up, we need to get the database that our program is going to be using, and the specific collection where we want to insert data and retrieve data from.
import pymongo
__author__ = "Your Name"
mongodb_uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(mongodb_uri)
database = client['fullstack']
collection = database['students']
Now we are ready to start inserting or retrieving data!
In order to retrieve data, you'll need to have some data in the collection. Refer to the top of this document for how to insert data into your students collection!
import pymongo
__author__ = "Your Name"
mongodb_uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(mongodb_uri)
database = client['fullstack']
collection = database['students']
students = collection.find({})
print(students)
If we were to run this program (run it!), we would expect to see a list of students or something recognisable. However, we do not. We see a bunch of gibberish that says something about a Cursor object. What is this?
The pymongo library does not give us back a list of whatever it found in the database. Instead, it gives us a Cursor. We can use this Cursor to find what is in the database. We can also do other things with this, as we'll see later. Things like sorting, for example.
In order to get our list of students, we need to iterate over the Cursor. We do this by using a for loop. Let's look at the structure of the loop, and then explain what is happening:
import pymongo
__author__ = "Your Name"
mongodb_uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(mongodb_uri)
database = client['fullstack']
collection = database['students']
students = collection.find({})
for student in students:
print(student)
This code reads nicely: "for each of the students in the students element, print the student".
The code is creating a variable called student, and assigning to it the first element found in the students object (our Cursor). Then it runs the "body" of the loop, which says print(student). After, it goes back to the top of the loop and gives the variable student the second value in our object. Then it repeats, with the third, fourth, etc., values.
Run the program, and you'll be able to see something recognisable!
Next, let's look at how we can simplify this and store the students in a variable, instead of printing them out.