Access MongoDB database with Python

Databases are not my subject matter expertise, nor do I have any real experience with any of the SQL or non-SQL based database platforms.

As I am putting more effort during my non-business hours into coding (Python), it is naturally beneficial to get some hands-on databases, the structure, and the way of storing and requesting data from databases. I heard about MongoDB long time ago, though, never really tried it, so it was about time to get some real insights.

MongoDB is a lightweight database management system that uses document based model to store data.

You have the option of installing MongoDB service on your own local machine, or somewhere in the cloud (free for now), and I chose the local way. The explanation below is for beginners with MongoDB (if you are a Pro, sorry, this is not for you).

First thing first, you need to import the ‘pymongo‘ python package:

from pymongo import MongoClient

Make sure the package has been installed upfront. Next step is to connect to the MongoDB service (in my case it is locally installed on my machine):

client = MongoClient('mongodb://localhost:27017')

Port number ‘27017‘ is the default where MongoDB service is listening for inputs. Hopefully your firewall understands that :).

Great, now what we have to do is, create a new database within MongoDB, and we are naming it as ‘database_leo

db = client['database_leo']

Now that we defined the database name, obviously you need to create a table within that database, we will call that table; ‘Employee‘.

employees = database.Employee

So far so good, we defined a Database and a Table within that DB. Let us look into the way on how to populate the table by inserting data into the table. In python we can do this by creating a variable of dictionary data type, where the indexes could be the ‘Name‘, ‘LastName‘, ‘Birth‘, ‘Country‘, and the values of these indexes are the name, last name, birth, and country of the employee itself.

employee = {
    'Name': 'Leonir',
    'LastName': 'Hoxha',
    'Birth': 1989
    'Country': 'Germany'
}

Once the employee data have been defined, next step is posting these data to the actual Mongo database table, a method called ‘insert_one()‘ is responsible for this action, and it is done as following:

employees.insert_one(employee)

As a result, if we execute this python code, the database table, and employee data will be visible. There is many ways to check this, and I do it with a GUI application, called MongoDB Compass which is quite convenient to browse databases and different tables. The picture below is an output from MongoDB Compass, and it shows exactly the Database named ‘database_leo’, table ‘Employee’, and the employee data as described in this post.

For your own convenience, here is the full python code that used for this very simple example of accessing a database and inserting some data.

from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017')
database = client['database_leo']
employees = database.Employee

employee = {
    'Name': 'Leonir',
    'LastName': 'Hoxha',
    'Birth': 1989,
    'Country': 'Germany'
}

employees.insert_one(employee)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.