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