MongoDB for Developers with Python Transcripts
Chapter: Course conclusion
Lecture: Lightning review: Document design
0:01 Next up was document design. Some of the concepts and ideas of relational databases still apply here,
0:08 you still are modeling data, you still put it into a database, but many of the techniques fall down, this whole concept of third normal form
0:16 doesn't make nearly as much sense as it does in a relational database. What more we focus on often is really
0:22 how do we make relationships either between documents or within documents. We saw the primary question, not the only one, but the most challenging one,
0:31 the one you have to think most carefully about is to embed or not to embed, and I gave you a few rules or tips to help you guide this decision.
0:39 One— is the embedded data wanted and you use it 80 percent of the time or more, most of the time when you get that containing document?
0:49 If that's true, you probably want to embed, if that's false, maybe consider that as a warning sign not to.
0:55 How often do you want the embedded document without the outer containing document?
1:00 If often what you really want to get access to is these little inside pieces, there's a lot of overhead and it really kind of complicates the way
1:08 you access it through your application, if you want to get them most of the time, or frequently, on their own. Is the embedded data abounded set?
1:17 Remember, these documents can only be sixteen megabytes or larger, the number is way higher than you really want it to be,
1:23 if this is an unbounded set you're going to continue to add to it, it very easily could outgrow the actual size that you're allowed to store.
1:29 Really for a performance reason though, is it abounded set and is that set small? Because if you put huge amounts of data in there,
1:37 you're going to really slow down your read time for these database operations that involve this document. These are the four main rules here,
1:44 you also want to consider how your application accesses this data, it might be really easy to answer these four questions
1:51 because there's a very constrained and small set of queries you run against your database;
1:56 or it could be that you ask all sorts of questions in a highly varied ways in which case it's harder to answer those questions,
2:03 the more types of queries you have the harder it is to know whether most of the time you want the embedded data for example.
2:09 The more varied your queries are, the more you'll trend towards third normal form, relational style and less embedding.
2:16 One of the situations where you have lots of varied queries is if you have this thing called an integration database,
2:22 which we talked about sort of sharing a database across different applications, versus having one dedicated to a particular application
2:28 where you can understand these questions very clearly. So when you're designing these documents
2:34 you want to really think most carefully about do you want to embed this data or create a soft foreign key type of relationship.