1 00:00:02,360 --> 00:00:06,510 Now that we had a look at a one-to-many relationship where embedding made sense, 2 00:00:06,680 --> 00:00:12,530 let's have a look at a relationship, a one-to-many relationship where splitting it in collections might 3 00:00:12,530 --> 00:00:14,740 make sense. Here 4 00:00:14,900 --> 00:00:22,460 I got a couple of cities like New York, Berlin and so on and I got the citizens, let's say we have a database, 5 00:00:22,460 --> 00:00:30,170 a collection which holds all the major cities of the world and then a list of every person living in 6 00:00:30,170 --> 00:00:35,910 that city, like the registry of the state where all people are registered, let's say. 7 00:00:36,200 --> 00:00:37,960 So we get something like this, 8 00:00:37,970 --> 00:00:39,700 the problem we'll face here, 9 00:00:39,800 --> 00:00:45,160 theoretically that would make sense to be a one-to-many relationship where you embed your documents 10 00:00:45,350 --> 00:00:53,030 but for one from application perspective, you might often be interested in fetching all the cities with 11 00:00:53,030 --> 00:00:58,220 their metadata but you don't want to fetch all the citizens along with the cities because in New York 12 00:00:58,220 --> 00:01:04,040 where we have like millions of people, that would be a lot of overhead data which we don't need which 13 00:01:04,040 --> 00:01:08,760 takes very long to fetch and which is a lot of data to transfer over the wire. 14 00:01:09,050 --> 00:01:15,590 And additionally for big cities with millions of citizens, you might indeed hit this 16mb limit 15 00:01:15,590 --> 00:01:17,550 for the overall document 16 00:01:17,630 --> 00:01:21,720 and keep in mind, that limit counts towards a document in nested documents, 17 00:01:21,740 --> 00:01:27,800 so it's not 16mb per nested element, it's 16mb for the overall document, 18 00:01:27,800 --> 00:01:32,130 so you might face this limit too or might hit this limit too. 19 00:01:32,330 --> 00:01:35,440 So for technical and application reasons, here 20 00:01:35,480 --> 00:01:37,830 splitting it up might make sense. 21 00:01:38,120 --> 00:01:44,990 So here, you might have your city data database and in there, you have your cities 22 00:01:47,860 --> 00:01:58,570 collection where you insert a city like the name, New York City coordinates which is a nested document 23 00:01:58,870 --> 00:02:00,150 with a latitude 24 00:02:00,190 --> 00:02:01,540 and this is arbitrary, 25 00:02:01,540 --> 00:02:07,310 this is not a real latitude and longitude, something like this 26 00:02:07,450 --> 00:02:09,810 and you store no data about the citizens in there, 27 00:02:09,850 --> 00:02:17,290 so you also have no citizens array in here with all the IDs of the citizens because that would be a super 28 00:02:17,290 --> 00:02:19,570 long array too if you have millions of citizens. 29 00:02:19,570 --> 00:02:21,460 So you just have the metadata 30 00:02:25,990 --> 00:02:26,790 and there 31 00:02:26,800 --> 00:02:29,190 I got to access square bracket. 32 00:02:29,410 --> 00:02:30,280 So you just have 33 00:02:30,280 --> 00:02:31,050 that stored 34 00:02:31,120 --> 00:02:36,170 and therefore if you find that one city, it looks like this. 35 00:02:36,170 --> 00:02:40,450 And you might also store the citizens but you will do this, 36 00:02:40,450 --> 00:02:47,500 citizens in a separate collection and then there you might have your different citizens with the name 37 00:02:47,720 --> 00:02:52,760 Max Schwarzmuller and the city ID maybe 38 00:02:52,890 --> 00:02:58,710 and that ID could be in New York City to match it by the name or you simply use that object ID of 39 00:02:58,710 --> 00:03:03,120 course, you want to use something which is unique so let's go for the objectId. 40 00:03:03,360 --> 00:03:08,730 So that would be one citizen and then we have another citizen where you also have a name, 41 00:03:11,740 --> 00:03:13,120 Manuel Lorenz and 42 00:03:13,190 --> 00:03:14,960 we're not really living in New York 43 00:03:14,980 --> 00:03:18,470 but just as an example here, just store that like this. 44 00:03:18,490 --> 00:03:23,710 Now insertMany needs square brackets around all the objects you're inserting, so don't forget to 45 00:03:23,710 --> 00:03:25,970 add these, hit enter 46 00:03:26,350 --> 00:03:32,030 and I got a typo, I was missing the closing parentheses after objectId. 47 00:03:32,520 --> 00:03:34,300 And now I inserted my citizens 48 00:03:34,570 --> 00:03:40,530 and if I inspect my citizens, if I find them all and I pretty print this, 49 00:03:40,600 --> 00:03:41,740 here are my citizens 50 00:03:41,800 --> 00:03:46,840 and now I can always retrieve just as citizens and match them to their city with the city ID if I need 51 00:03:46,840 --> 00:03:53,920 to or fetch just the cities and I'll not hit the document limitation of 16mb for the city because 52 00:03:53,920 --> 00:04:00,210 I don't embed all my citizens and I also don't unnecessarily fetch all the citizens along with the city 53 00:04:00,280 --> 00:04:02,290 if I'm only interested in the city metadata.