MongoDB for Developers with Python Transcripts
Chapter: Modeling and document design
Lecture: Introduction to document design

Login or purchase this course to watch this video and the rest of the course contents.
0:01 We've come to a pretty exciting part in the course, we're going to talk about document design and modeling with document databases.
0:09 So let's take a step back and think about relational databases. There is in fact a couple of really systematic, well known,
0:16 widely taught ways of modeling with relational databases; there's still a bit of an art to it, but basically it comes down to
0:25 third normal form, first normal form, some of these well known ways to take your data, break them apart, generate the relationships between them,
0:34 so if we're going to model like a bookstore with publishers and users who buy books at the bookstore,
0:39 and they rate books at the bookstore, it might look like this— we have a book, the book would have a publisher,
0:45 so there is a one to many relationship from publisher to books, you can see the one on the star and the little relationship there,
0:51 and we have some flat properties like title and published and publisher id for that relationship, and similarly,
0:56 we have a navigational relationship over to the ratings, so a book is rated, so the ratings would have almost normalization table
1:04 or many to many table there has the book id and the user id and then the value and we just happen to have a auto increment id there,
1:11 it's not necessarily the way we have to do it, we could have a composite key, we've got our user
1:16 and the user can go navigate to the ratings, and things like that. Now, of course, this is a very simplified model
1:21 in a real bookstore with real ecommerce happening and all that and categories and pictures and all those things, this would be way more complicated,
1:29 but the whole idea going forward is going to be pretty similar and I think keeping it simple enough that you quickly understand the model
1:35 and don't get lost in the details, is the most important thing here. So this more or less follows third normal form here.
1:41 in terms of how we're modeling this in the relational database. Could we move this to MongoDB, could we move this to a document database—
1:48 sure, we could have exactly the structure. Now those relationships, those are not full on foreign key constraints,
1:53 those would be loosely enforced, not enforced in the database but enforced in the app relationships between the what would be collections;
2:01 but certainly, we could do this, is it the best way though? The answer is usually not, maybe, but probably not.
2:08 So what we're going to focus on now is how do we take our traditional knowledge of modeling databases and relational databases
2:15 and how does that change, what are the trade-offs we have to deal with when we get to a document database.
2:21 So the good news is, usually things get simpler in document databases in terms of the relationships, you might have
2:28 what would have been four or five separate tables with relationships, it might get consumed into a single item,
2:35 a single collection or single document really, so here this is how we're going to model our bookstore
2:41 that we just looked at in third normal form, but now in a document database. And really, the right choice here comes down to
2:47 how is your app using this data, what type of questions do you usually ask, what's the performance implications, things like this.
2:54 So now we have a books, we have a publisher and a user and these have similar top level items, and we do have some traditional relationships.
3:02 So there's a one to many relationship between publisher and books theoretically we can embed the book into the publisher
3:09 but there's many, many books for some publishers and that would be really a bad idea;
3:13 so we have this traditional relationship, like you might have in a relational database.
3:16 Now again, not enforced by Mongo, but enforced by your app, so same basic idea. Next up, we have the ratings, remember we have that
3:23 like many to many table from users to book ratings, now that has actually moved and now we're storing these items
3:31 in an embedded array of objects inside the book table, or the book collection. So now each book has a ratings array, it has the number of ratings,
3:40 those are just put right in there, so is this the right design— maybe,
3:44 it's certainly a possible design, and it's the design that we're going to go with
3:48 for our examples, but we'll talk about when it's actually the right design. And I'll help you make those trade-offs next.

Talk Python's Mastodon Michael Kennedy's Mastodon