1 00:00:02,210 --> 00:00:08,450 Here's example number one, we had a kind of related example earlier in this course already. Let's 2 00:00:08,450 --> 00:00:15,470 say we're creating our database, our application for a hospital and there, we got patients and there, disease 3 00:00:15,560 --> 00:00:16,760 summary. 4 00:00:16,760 --> 00:00:22,660 So every patient has one disease summary which belongs to that patient only 5 00:00:22,760 --> 00:00:25,230 and there is only one summary per patient. 6 00:00:25,250 --> 00:00:28,110 So we're not talking about the history we had earlier in this course, 7 00:00:28,130 --> 00:00:29,630 it's just a summary, 8 00:00:29,640 --> 00:00:36,470 something like is ill, had previous diseases and a list of previous diseases, things like that. 9 00:00:36,470 --> 00:00:38,160 So one summary per patient 10 00:00:38,270 --> 00:00:43,640 and of course this is a one-to-one relation because the summary of patient A can never belong to patient 11 00:00:43,640 --> 00:00:44,160 B 12 00:00:44,390 --> 00:00:52,940 and the other way around, how could we model that? Back in a brand new cleaned up database server, I'll 13 00:00:52,950 --> 00:00:57,040 switch to my hospital database which will be created on the fly 14 00:00:57,470 --> 00:01:04,530 and there, I want to create my patients collection or to be precise, I want to insert a patient, so let's insert 15 00:01:04,640 --> 00:01:06,130 one patient. 16 00:01:06,240 --> 00:01:14,050 Now let's say a patient simply has a name Max, an age and then we have that diseaseSummary, 17 00:01:14,060 --> 00:01:23,390 now let's go for the reference approach first, diseaseSummary could be summary-max-1, any identifier 18 00:01:23,390 --> 00:01:25,480 you want, 19 00:01:25,480 --> 00:01:28,480 so here I'll go for that ID approach. 20 00:01:28,690 --> 00:01:29,790 We can insert that 21 00:01:29,860 --> 00:01:38,920 and obviously if I now inspect my patients with findOne pretty, we see that one patient in there, without 22 00:01:38,920 --> 00:01:41,240 pretty though, that is not existing there, 23 00:01:41,290 --> 00:01:43,940 so we see the one patient in there. 24 00:01:43,960 --> 00:01:52,630 Now let's also create another collection, diseaseSummaries and then there I'll also insert one summary, 25 00:01:52,800 --> 00:01:59,740 there I'll explicitly define the ID and use my own ID, summary-max-1, I could have also gone with the 26 00:01:59,770 --> 00:02:01,790 automatically generated objectId 27 00:02:01,930 --> 00:02:06,730 but then I should have generated the summaries collection in that entry first so that I could take 28 00:02:06,730 --> 00:02:10,150 that objectId and use it as an ID on the patient. 29 00:02:10,150 --> 00:02:11,460 I did it the other way around, 30 00:02:11,500 --> 00:02:13,870 so I'm using my own ID here 31 00:02:14,140 --> 00:02:21,890 and there I'll simply add my diseases array which simply lists all diseases 32 00:02:21,910 --> 00:02:27,430 I ever had and let's say that's a cold and that's a broken leg which I never had 33 00:02:27,430 --> 00:02:37,320 but anyways. So now I added this too and now of course I can access my diseaseSummary, summaries collection here, 34 00:02:38,280 --> 00:02:43,170 find the one element that is in there and this is the element I just inserted with my own ID and 35 00:02:43,170 --> 00:02:48,040 that ID of course matches the ID I stored as a reference on my patient. 36 00:02:48,250 --> 00:02:53,180 Now let's say in our application, the website we're building, the mobile app, whatever it is, 37 00:02:53,340 --> 00:03:00,690 we now have a request where we need that patient and also the disease history or the disease summary I should 38 00:03:00,690 --> 00:03:01,360 say. 39 00:03:01,860 --> 00:03:08,160 So we might have an incoming request which in the end by the mongodb driver is translated to a 40 00:03:08,160 --> 00:03:09,800 find request, 41 00:03:09,810 --> 00:03:16,770 so we first of all find our patients and in reality, we'll probably do that by looking for let's say 42 00:03:16,770 --> 00:03:23,370 all patients with a name of Max or here as a shortcut since I only have one, I'll just use findOne, 43 00:03:23,370 --> 00:03:27,310 the thing is in the end we have some query that returns Max here. 44 00:03:27,780 --> 00:03:30,420 So that is my returned 45 00:03:30,600 --> 00:03:31,610 patient data, 46 00:03:31,650 --> 00:03:34,750 the problem is I'm also interested in the diseaseSummary, 47 00:03:34,890 --> 00:03:37,000 so I also want to get that. 48 00:03:37,230 --> 00:03:46,350 Now what I can of course do is in my application, I can access diseaseSummary and therefore get my key 49 00:03:46,350 --> 00:03:54,180 here and in the application we're writing, in PHP, .NET, Java, NodeJS, whatever it is, we can of course 50 00:03:54,300 --> 00:03:58,120 fetch some data from a server and then store that data in a variable, 51 00:03:58,140 --> 00:04:07,240 so let's say here in the shell, we can do this with the var keyword, we have our dsid for diseaseSummary 52 00:04:07,240 --> 00:04:15,280 ID and that simply is the result of patients findOne and then there, accessing the diseaseSummary, 53 00:04:15,460 --> 00:04:24,100 so now if we output dsid, that is the ID stored in this patient. And now we can simply reach out to 54 00:04:24,100 --> 00:04:27,130 diseaseSummaries, so that other collection 55 00:04:27,370 --> 00:04:37,800 and there we want to find the first diseaseSummary where _id is equal to dsid, 56 00:04:37,830 --> 00:04:39,630 so I can use that variable here 57 00:04:39,870 --> 00:04:45,810 and I simply stored it in a variable so that I don't have to paste or add this long term here as a value 58 00:04:45,810 --> 00:04:46,580 for ID. 59 00:04:46,920 --> 00:04:49,640 If I do that, I indeed find my diseaseSummary, 60 00:04:49,650 --> 00:04:52,420 the bad thing here is that this took two steps 61 00:04:52,530 --> 00:04:56,060 and also from a performance perspective, this will actually be super fast 62 00:04:56,070 --> 00:05:01,640 but if you have a very big database, a very big collection of patients and a big collection of 63 00:05:01,640 --> 00:05:02,580 diseaseSummaries, 64 00:05:02,580 --> 00:05:08,880 this might not be the optimal way of doing that, splitting that across two collections feels a bit unnecessary 65 00:05:08,880 --> 00:05:09,870 here, 66 00:05:09,870 --> 00:05:15,380 the better approach in such a case where we have a strong one-to-one relation would be to use embedded 67 00:05:15,380 --> 00:05:16,780 document. 68 00:05:16,800 --> 00:05:24,660 so let me actually reach out to my patients and delete all patients in there 69 00:05:24,930 --> 00:05:27,440 and now let's insert that patient again, 70 00:05:27,570 --> 00:05:31,370 so that insert one command from earlier where I inserted Max, 71 00:05:31,560 --> 00:05:38,480 now I will not insert a diseaseSummary which is a reference but instead I'll insert a nested, an embedded 72 00:05:38,490 --> 00:05:47,400 document here and now that embedded document would have my diseases key, my diseases field with the 73 00:05:47,400 --> 00:05:51,500 cold and the broken leg, like this. 74 00:05:51,720 --> 00:05:56,910 And now the huge advantage of course is that in the application we're writing with whichever language 75 00:05:56,910 --> 00:06:05,760 we use, I can simply write my query to find my patient by ID, by name or in this case as a shortcut, simply 76 00:06:05,760 --> 00:06:09,060 with findOne and I will get all the data I need, 77 00:06:09,060 --> 00:06:11,850 the patient data and the diseaseSummary data. 78 00:06:12,060 --> 00:06:17,870 So this is an example for when you would use an embed document, you have a strong one-to-one relationship.