Getting Started with Python and MongoDB

Disclaimer: This article has some of my personal style in it. Be aware that during the course of this article that my tongue is planted firmly in my cheek.

So there you are. You’re a starting Pythonista and you’re on your way down the trail, learning how to make things. Then, you realize that you want to make data persist after the application is done running. The kick is that you don’t now how to use a database and using a text file or XML is lame. Good news! This tutorial is here to teach you how to use a database, specifically MongoDB with Python.

Admittedly, if you don’t use Python, this tutorial is gonna be kind of a bust for you. If you want to know how to set-up MongoDB with JavaScript or C#, hit me up on Twitter. Alright so we’ve accepted our mission statement. Become an expert with Python and MongoDB.

First thing you need to do is to install MongoDB on your system. I’m not going to go over that in this tutorial. You can find those steps here: Once you’ve installed Mongo on your machine. The first thing that you’re going to do is you’re going to start it up. You’re going to open a command terminal up and type the following command. The command below starts the database.

The database has to be started so that it can accept data from the Python application that you’re writing. Otherwise your application isn’t going to do anything but send data to nowhere. Into the ether.

Keep that command prompt open, because we need it for the next part of the program we’re gonna write. We need to pip install a specific module. Note that I am aware that most best practices say to create a virtual development environment, but we don’t do that here. Type the following command into your prompt.

Look at that. Second bit of self-typed stuff and we’re well on our way to getting your first application with Python to work with MongoDB. So what this little module will do is give you access to the MongoDB functionality though an API that is connected though this library.

We’ve only got a few steps left then you can start writing real applications. In order for your application to access your database, we’re gonna want to create a new class. This will be your database connection class. Then you’re gonna create your class for your database functions.

First Class:

In the class above we’ve defined that we’re going to be importing the pypi module that we just installed off of pip. The class then names the client and attaches it to a localhost before then naming the database. Of course you’re going to change the name of your database to whatever you want it to be.

Next thing that we’re going to do with the application is that we’re going to be calling upon this class in the next class. We’re then going to use the db from the client to make a collection. MongoDB isn’t an SQL database, its document based, so there are no tables there are collections of documents.

You’re going to use the class above as a call whenever you want to use the database to create a controller for the data that you’re going to be putting into the database that is done like below in the following code. This class below is going to be based around simple crude functionality.

Second Class (ignore the ‘\’ it happened after the code was tested):

Alright there you go. That’s the class that is going to handle your database functions. Now all you’re gonna do is call that class in your other classes to pull things into and from your database or remove items.

An example would be a file that you want to pull a listing of a house from a database and then do some mutations on it. You’d import this class, instantiate it in the __init__ method. Then you’d call the object you want in either the init method or within another method.

Related Posts