1 00:00:02,340 --> 00:00:08,520 So we had a look at the general data structure but always only for one collection and the documents 2 00:00:08,520 --> 00:00:12,280 in that the one collection. Typically you have multiple collections though, 3 00:00:12,480 --> 00:00:16,470 you might have users and products and orders. 4 00:00:16,470 --> 00:00:23,490 So if you have multiple collections that still are related of course or where the documents in these relations 5 00:00:23,490 --> 00:00:24,580 are related, 6 00:00:24,690 --> 00:00:29,570 you obviously have to think about how do you store related data, 7 00:00:29,760 --> 00:00:37,650 do you use embedded documents because that of course is one way of reflecting a relation, if a user 8 00:00:37,710 --> 00:00:41,640 has an address and that address consists of a street and a city, 9 00:00:41,730 --> 00:00:45,680 then you might have a nested address document in the user document 10 00:00:45,870 --> 00:00:48,590 or do you work with references? 11 00:00:48,630 --> 00:00:49,930 Here are two examples, 12 00:00:50,130 --> 00:00:54,580 as I just said, we might have a nested document and an embedded document, 13 00:00:54,600 --> 00:00:55,870 here are my customers 14 00:00:56,040 --> 00:01:02,160 and by the way of course in json, the field names like username, age and so on would be enclosed in double quotation 15 00:01:02,160 --> 00:01:02,790 marks, 16 00:01:02,790 --> 00:01:05,510 I'm just leaving that out here to save some space, 17 00:01:05,520 --> 00:01:09,460 so this is not a real json code but it is structure of the data. 18 00:01:09,690 --> 00:01:15,570 And here, my customer has an address and that address could totally be stored in a new collection 19 00:01:15,570 --> 00:01:22,170 but we can also embed it into our user or into our customer here because it's strongly related to that 20 00:01:22,170 --> 00:01:28,590 customer and we might want to fetch both at the same time or at least have an easy way of fetching both 21 00:01:28,710 --> 00:01:30,320 if we need to. 22 00:01:30,330 --> 00:01:33,180 The alternative is that you use references, 23 00:01:33,180 --> 00:01:40,530 let's say we still have customers and each customer has a list of his favorite books and we could absolutely 24 00:01:40,530 --> 00:01:43,390 go with nested or embedded data. 25 00:01:43,410 --> 00:01:49,860 So here we have an array with embedded documents and each document is shortened here but each document 26 00:01:49,860 --> 00:01:54,840 could be a book with a title, a price, an author and so on. 27 00:01:54,840 --> 00:01:59,790 Now the problem here is that we will have a lot of duplicate data because different customers can have 28 00:01:59,790 --> 00:02:01,800 the same favorite books 29 00:02:01,800 --> 00:02:07,000 and if we then change something about that favorite book, we'll have to change it on all customers, 30 00:02:07,020 --> 00:02:12,970 so instead here, we might go for references. We might have a customers collection and the books collection 31 00:02:13,500 --> 00:02:20,730 and in the customer, we still have that array of favorite books but there, we only store the IDs of the 32 00:02:20,730 --> 00:02:22,070 favorite books. 33 00:02:22,110 --> 00:02:26,220 That means that when we fetch the customers and we also want to fetch the favorite books for 34 00:02:26,220 --> 00:02:29,760 the customer, we have to run two queries, that is correct 35 00:02:29,970 --> 00:02:35,140 but on the other hand if we change a book, we don't have to change it on 10 different customers but only 36 00:02:35,140 --> 00:02:38,220 in the books collection and the ID will never change, 37 00:02:38,220 --> 00:02:43,590 so we never have to change or touch the favorite books array in the customers document, 38 00:02:43,860 --> 00:02:47,070 so we have that relation through a reference. 39 00:02:47,070 --> 00:02:53,370 Now you got these two different approaches and now the obvious question is when should you use which. 40 00:02:53,910 --> 00:02:56,190 Time to have a look at some examples.