MongoDB for Developers with Python Transcripts
Chapter: What is NoSQL?
Lecture: Working with document DBs and queries styles
0:01 So let's talk about how document databases work. Here's a record from my actual online training platform
0:10 chapter 1001 from 'The Python Jumpstart by Building Ten Apps' course and this is more or less exactly what came out of the database,
0:20 with a few things taken away so it actually fits on the slide here. Now, let's break this into two pieces here, this green piece and the blue piece.
0:30 First of all, you can see we have json, when you work with document databases, you'll frequently run into json
0:36 as at least the visual representation of the record. In fact, in MongoDB it doesn't really work in terms of json
0:47 it works in something called bson or binary json, so this binary tokenised type typefull, rich typed version
0:58 of sort of extended json but already tokenised and stored as a binary version; this is what's transferred on the wire and to some degree
1:08 this is what stored actually in the database so how it actually get stored is it moves around and the database storage engines are changing
1:17 and sort of plugable now in MongoDB, but more or less you can think of this becoming a binary thing and then stored in the database.
1:25 When it comes over into say Python, we'll of course map this into something like let's say a Python dictionary
1:32 or a type that has these fields and so on. So if you look at the green area, this is just the jasonified version of any other database record,
1:42 it has let's think of it as columns if you will for a minute it would have columns here like one would be the id
1:50 one would be the title, one might be course id and it has values; and that's all well and good, until we get to lectures,
1:55 and here's where the power of document databases comes in, lectures is not just like seven, there are seven lectures or whatever
2:02 no, lectures is a list, so multiple things, and each one of those things is a lecture, an individual one, with its individual fields
2:14 so id, title, video url, duration in seconds, again there's actually more to it, but so it fits on screen right;
2:19 with this document database, you can think of these things as kind of pre-computed joints, and this solves a ton of problems
2:28 and makes the NoSQL aspect of document databases super powerful. So it makes this chapter more self contained, if I want to get this chapter back,
2:36 instead of going to the chapter and then doing a join against the lectures and maybe some other type of join,
2:43 and you're getting a bunch of different pieces and pulling them back together I just might do a query, find me the chapter with id 1001
2:51 bam, it's back, I've got the whole thing and so you can think of this as like pre-joined data if 80, 90 percent of the time I'm working with a chapter,
3:00 I care about the lecture data being there, why not store it in a way that it's already bound together, so I don't have to do that join,
3:08 I don't have to do multiple queries or things like this. Okay, so this is really powerful and we'll talk a lot
3:15 about when this makes sense, when it does not make sense and so on, but this means that if I take the single record
3:22 and I put it on some server, even if I've got like ten servers and some sort of horizontal scale situation
3:28 and I do a query by chapter id, I don't then have to go back to the cluster find where all the lecture data lives or anything like that.
3:35 No, it's just bringing that one record brings most of the data that I need to work with
3:40 when I'm working with a chapter, right along with it, which is excellent. That's the benefit, the important question the critical question to say
3:48 like is this going to work for our database system as a general thing is well can I ask the questions that I would still have asked
3:55 if lectures was its separate table, if it was a flat table just like relational databases. So, what if I want to find his last lecture here, 10 106,
4:07 will I be able to go to the database and say hey document database, I would like to get lecture 10 106 and I want to do that with an index
4:18 and I want to have it basically instantaneously, even if there's a million records, I want to instantaneously get the record
4:24 that embedded deep down within it could be many, many levels not just one, right, but in this case it's just one;
4:31 I want to get the record that deep down within it somewhere matches the fact that the lecture id is 10 106.
4:38 And the answer is for the document databases yes, so this makes them very, very different than the key value stores just doing a json blob
4:47 because we can ask these very interesting questions, we can do map reduce or aggregation type things for big data,
4:53 analysis and analytics, all sorts of stuff is possible, because you can actually query deeply down into these nested objects.
5:02 So that's how document databases work, and we'll explore the when, whys and hows of designing these documents
5:09 when we get to the document design chapter.