Journey to the centre of the database: a tutorial-tale about accessing MongoDB with Python (Part 1)

Hi everyone,
today I want to tell you a story and I also want to teach you something with a small tutorial.. And I'm going to do both at the same time! Here's in fact a small experiment that we'll call tutorial-tale: i.e. stories that hide a tutorial about something (or tutorials embedded into a story if you prefer).
As this is the first one, it will be on one of my favourite topic: performing some basic operations on my favourite database (MongoDB) using my favourite programming language (Python).



Once upon a time,
there was a hero called User, that was deeply in love with the princess of DataLand. The princess loved him as well but they were kept apart by the evil uncle of the princess: NoNo. Uncle NoNo infact was the main exponent of a movement that took to the extreme the concept of "Non-relational" and - misunderstanding what it meant even for databases - extendend it to people too, prohibiting relations among people. He didn't want his niece to marry User, so he kidnapped her and kept her hidden in a mysterious location.   
User was desperate and decided to go on a journey to find his one true love with his trusted companion: a magic snake called Py. 

In the realm of DataLand there were a lot of oracles, called databases, that stored every kind of knowledge. Knowing the passion of Uncle NoNo for Non-relational databases, User knew for sure that he was going to find informations about were the Princess was kept inside the most famous Non-relational oracle: MongoDB. 

User knew that he could ask questions to MongoDB using an enchanted shell, but Uncle NoNo was clever and collected all the shells of the realm and destroyed them, to avoid people querying MongoDB using them. 
Luckily for User, his friend Py came to his help "Don't worry my dear friend, I ssssspeak to databasesssss too and I can query MongoDB in your place. I just need a couple of artifactsssss and a piece of paper where I can write all the magic sssssspells in sssssequence.". User was happy and confused at the same time: "What artifacts and what spells?" he asked to the snake. "Sssssome basic ssspells called 'line of code', I need to casssst them in order so I need to write them down (I'm a magic snake so I can write even though I have no hands). But I can't perform the more powerful and articulate ssspells all alone, I need some artifactsss, called librariessss, that contains books filled with these spellssss." 

So Py took a special shell he had - that worked only for snakes - and said:  
 
python -m pip install pymongo

Magically the libraries appeared in User's hands. "Well", said Py, here's the first lines of code:


import pymongo
from pymongo import MongoClient
from bson.son import SON

"Now, these libraries will do the hardest part of the job in our place" said User "but how do we speak to MongoDB?" "We need to connect to him", said Py adding other lines of code:

client = MongoClient('localhost', 27017) 
"We create a client, that is the necessary way people like you, Usssser, and me can communicate with a database, we can't accessssss directly as too many knowledge will overload us and may harm the database itself. As we want to sssspeak with MongoDB, our client will be a MongoClient, but for working it also needsss to know the addresssss of MongoDB so he can interact with the correct database. Ssssince we know it issss on our same realm (i.e. local machine), we ssssay that the address is localhost, and then we ssssay also the number of the port, because in a realm there may be sssseveral different oracles/database running. Now my dear friend, what informationssss do you exactly need from MongoDB?"

 "Well, the Oracle stores information about all the people in the realm. I want to know where the Princess is, so it may be helpful to access to the log of the recorded positions of all the people."
"Okay! Let'sss do it!" said Py, writing down two more lines of code:

db = client['db_people_of_this_realm']
collection=db.positions


"Here we are" said the snake "You have a collection of documentsss about all the positions of all the people in this realm: how do you find the Princessss?" 

To be continued.... in part 2!



Commenti