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

Hi everyone!
In part 1 of this tutorial-tale we left our friend User and his companion Py reasoning about what question they should ask to the database for finding the Princess. Here's what happened next!

"Oh!" exlaimed User when he saw how many documents appeared floating in front of them: they where more than a billion pieces of paper, and each of them contained some text written in a strange language. User was enchanted, "How can I see what informations these documents contains? I need to find those about the Princess!". Py nodded and added another line of code:
collection.find_one()

 One document flied away from all the others and went directly in front of User and Py. User grabbed it and read it out loud:

{

_id: 1234,
name: Mickey Mouse,
location: Corn Lake,
date: 2016-08-01
}


"What language is this? And what do these data mean?" asked User. "Well, my friend, each database adoptsss a language based on the kind of information it sssstores. This is JSON, the language that MongoDB uses for itsss data. My magic allows me to understand any language assss long I have the proper artifacts, but I think you sssshould be able to translate it easily even if you speak only human language. This document contains informationsssss about Mickey Mouse - I think we met him once, he'sss a nice guy - that on the 1st August 2016 was located at Corn Lake."
"Ok, cool. But.. honestly, I don't care. Why did you cast that spell? How will this document about Mickey help me in finding the Princess?" asked User.
"It will. Don't worry! Any document in a collection has the ssssame structure and we need to know the sssstructure of the document for casting the correct sssspell and retrieve the informations about the Princessss. Think about a doctor: for visiting you, User, he need to know where each of your body part is.. and he knowssss it even if he has never seen you before, because he knowssss you belong to the human race and he has studied other human being before visiting you. Does it makes sensssse to you?"
"A bit. So if I got it right, we're looking for a document like this, that contains informations about my Princess. There is a spell for having all the documents about a person? I mean, a person that has a certain name... Princess... " asked User, getting more and more excited as he felt he was close to find his love.
"Be careful of what you ask!" exclaimed the snake, casting another spell:

collection.find({"name": "Princess"})

One document flipped away from the others, User grabbed it quickly but in that moment another one flied toward him, then other ten, and another hundred and in few moments more than ten thousand documents where floating in the air around User! "Help me Py! Help! What's happening?" he screamed, but in another blink of an eye all the documents fell to the ground. User sat upon them in relief. "These are the answerssss to your question. All the document about the Princessss. A document for each place she has ever been. Is that what you wanted?"
"NO! Not at all!" User raised his voice, now he was getting angry and sad at the same time, as he was losing hope in his friend. "I want to know where the Princess is NOW. I don't care of all the rest! Does this mean I should go through each of these documents to find it out? It will take me a lifetime, but I will!"

The snaked looked at him seriously, then suddenly he started laughing very hard. "Calm down, User, calm down. There's no need to look among the documentsss one by one. I skipped a little bit of the sssspell just to teach you a lesson: you asked for all the documents about the Princessss and that's what you obtained. You have to be careful when you write linesss of code, you have to be precise!"
"What? You tricked me?"
 "No.." said the snake smiling "you just asked the wrong question! What do you really need to know?"
"Uhm...." User rubbed his nose, as he did anytime he was thinking seriously about something "I don't need to know any place the Princess has ever been, I just need to know the most recent!"
"Exactly! So you need to retrieve all the documents about the Princesssss, sort them according to the date and at last you need to pick the last one!" . And so the snake wrote one last spell:


collection.find({"name": "Princess"}).sort({"date": -1}).limit(1)

One document flied into User's hands:

{

_id: 9876,
name: Princess,
location: Mount Jelly,
date: 2016-11-14
}

"The Princess is in Mount Jelly! We have to go there! Hurry!" exclaimed User, ready to go and rescue his love.


Will he find her? Will they live happily ever after? You'll find out next time!

Commenti