1 00:00:01,100 --> 00:00:03,780 Let's now use a process called populate 2 00:00:03,780 --> 00:00:06,610 in order to actually get access to the referenced 3 00:00:06,610 --> 00:00:10,173 tour guides whenever we query for a certain tour. 4 00:00:11,820 --> 00:00:14,290 So, in the last video, we created a reference 5 00:00:14,290 --> 00:00:17,660 to the user, here in this guides field, 6 00:00:17,660 --> 00:00:20,180 right in our tour model, right? 7 00:00:20,180 --> 00:00:22,840 And now we're gonna use populate in order to 8 00:00:22,840 --> 00:00:25,610 basically replace the fields that we referenced 9 00:00:25,610 --> 00:00:28,210 with the actual related data. 10 00:00:28,210 --> 00:00:30,330 And the result of that will look as 11 00:00:30,330 --> 00:00:32,920 if the data has always been embedded, 12 00:00:32,920 --> 00:00:35,100 when in fact, as we know, it is in 13 00:00:35,100 --> 00:00:38,280 a completely different collection okay? 14 00:00:38,280 --> 00:00:41,940 Now, the populate process always happens in a query. 15 00:00:41,940 --> 00:00:44,713 And so let's now go to our tour controller, 16 00:00:46,000 --> 00:00:50,340 and then right to the function where we get a single tour. 17 00:00:50,340 --> 00:00:52,460 So here, in get tour. 18 00:00:52,460 --> 00:00:55,440 And here's our query, right? 19 00:00:55,440 --> 00:00:57,831 So this here, is where we build our query. 20 00:00:57,831 --> 00:01:00,450 And now all we need to do is to add 21 00:01:00,450 --> 00:01:02,183 the populate to the query. 22 00:01:04,370 --> 00:01:07,440 So populate, and then the name of the field 23 00:01:07,440 --> 00:01:09,250 which we actually want to populate 24 00:01:10,490 --> 00:01:12,533 and that is called guides, okay? 25 00:01:13,950 --> 00:01:17,850 And if you think about it, this name actually makes sense. 26 00:01:17,850 --> 00:01:21,080 So we want to populate so basically to fill up 27 00:01:21,080 --> 00:01:24,940 the field called guides in our model. 28 00:01:24,940 --> 00:01:27,870 Right, so again, this guides field 29 00:01:27,870 --> 00:01:29,860 only contains the reference. 30 00:01:29,860 --> 00:01:32,620 and with populate we're then gonna fill it up 31 00:01:32,620 --> 00:01:36,750 with the actual data, all right and again only in the query 32 00:01:36,750 --> 00:01:39,890 and not in the actual database, okay? 33 00:01:39,890 --> 00:01:41,840 So, this is really all we need to do. 34 00:01:41,840 --> 00:01:46,760 And let's now take a look at the result, okay? 35 00:01:46,760 --> 00:01:51,760 So, this is the id of the tour and let's remember 36 00:01:51,840 --> 00:01:55,370 that this is how the guides look right, right now, okay? 37 00:01:55,370 --> 00:01:59,323 But if we now, get tour and with that id, 38 00:02:01,170 --> 00:02:03,260 then let's now wait for it, 39 00:02:03,260 --> 00:02:07,220 and indeed, here is the data about the guides. 40 00:02:07,220 --> 00:02:10,720 Okay, so these two elements that we had in the guides 41 00:02:10,720 --> 00:02:14,600 already, have now been populated with the actual data. 42 00:02:14,600 --> 00:02:17,373 And if we now take a look at get all tours, 43 00:02:18,550 --> 00:02:21,933 let's actually get rid of some of this stuff here. 44 00:02:22,842 --> 00:02:25,073 We have too much stuff open here. 45 00:02:26,010 --> 00:02:28,423 So if we now get all the tours, 46 00:02:29,730 --> 00:02:31,820 okay, then let's scroll to the end 47 00:02:31,820 --> 00:02:33,280 where the new one should be 48 00:02:36,030 --> 00:02:38,590 and apparently it's now well, 49 00:02:38,590 --> 00:02:40,690 oh that's because I have this filter here, 50 00:02:41,840 --> 00:02:46,610 let's get rid of that, and now I have my 10 results 51 00:02:46,610 --> 00:02:49,270 and actually it's right here, so the new test tour. 52 00:02:49,270 --> 00:02:52,290 And now you see that in here it's back to showing us 53 00:02:52,290 --> 00:02:55,860 only the values that are actually in the database. 54 00:02:55,860 --> 00:02:58,010 So let's also confirm that here in Compass. 55 00:03:01,940 --> 00:03:05,200 And so indeed, as you see, we still do not have the actual 56 00:03:05,200 --> 00:03:09,750 data in here in the database, but only really the ids. 57 00:03:09,750 --> 00:03:13,290 Okay, and it's only that populate step which will then 58 00:03:13,290 --> 00:03:16,540 replace these ids with the actual data, okay? 59 00:03:16,540 --> 00:03:18,740 And so right here, that doesn't happen 60 00:03:18,740 --> 00:03:21,260 well because, we didn't implement the populate 61 00:03:21,260 --> 00:03:24,200 in the get all tours route handler. 62 00:03:24,200 --> 00:03:27,950 Okay, now let me just show you a small trick 63 00:03:27,950 --> 00:03:30,490 that we can do with the populate function. 64 00:03:30,490 --> 00:03:33,590 Which is to actually also just select the certain fields. 65 00:03:33,590 --> 00:03:38,370 So for example, we're not interested in this v property here 66 00:03:38,370 --> 00:03:41,350 and also not in passwordChangedAt right, 67 00:03:41,350 --> 00:03:42,683 so that's not the kind of data 68 00:03:42,683 --> 00:03:45,660 that we want about our tour guides. 69 00:03:45,660 --> 00:03:49,197 And so here in populate, we can actually specify that. 70 00:03:49,197 --> 00:03:52,230 So instead of just passing in the string, 71 00:03:52,230 --> 00:03:55,280 we can create an object of options 72 00:03:56,620 --> 00:03:57,793 and then we can say, 73 00:03:59,440 --> 00:04:02,850 the path is guides, so basically, the name of the field 74 00:04:02,850 --> 00:04:07,850 we want to replace, and then, as usual, we can use select, 75 00:04:09,800 --> 00:04:11,553 and then minus, 76 00:04:12,710 --> 00:04:16,970 this one here and also, the other one, 77 00:04:16,970 --> 00:04:21,790 so passwordChangedAt, it's just easier to copy, 78 00:04:21,790 --> 00:04:25,370 all right, so let's change it here, 79 00:04:25,370 --> 00:04:27,603 and send this again. 80 00:04:28,600 --> 00:04:31,800 And this is of course, not the right route, 81 00:04:31,800 --> 00:04:35,910 so sorry for that, I think I also used it before actually 82 00:04:35,910 --> 00:04:38,143 where really I wanted to show you this one, 83 00:04:39,040 --> 00:04:42,970 right, and so sorry for that, but still 84 00:04:42,970 --> 00:04:45,920 what we want to get rid of here is this V 85 00:04:45,920 --> 00:04:47,510 and this passwordChangedAt. 86 00:04:47,510 --> 00:04:49,230 And so we already did that, 87 00:04:49,230 --> 00:04:51,737 so if we send it now, then of course 88 00:04:51,737 --> 00:04:55,810 we only get the data that we're interested in. 89 00:04:55,810 --> 00:04:58,540 All right, so this populate function 90 00:04:58,540 --> 00:05:01,110 is an absolutely fundamental tool 91 00:05:01,110 --> 00:05:03,160 for working with data in Mongoose. 92 00:05:03,160 --> 00:05:04,790 And especially of course when there 93 00:05:04,790 --> 00:05:06,760 are relationships between data, 94 00:05:06,760 --> 00:05:09,860 okay, so you should always know exactly how and when 95 00:05:09,860 --> 00:05:12,220 to use it for your own applications. 96 00:05:12,220 --> 00:05:15,040 And in order to train for that, we will of course use it 97 00:05:15,040 --> 00:05:18,200 many times more throughout this section. 98 00:05:18,200 --> 00:05:21,140 Now just one thing that I want you to keep in mind, 99 00:05:21,140 --> 00:05:24,280 is that behind the scenes, using populate will still 100 00:05:24,280 --> 00:05:26,300 actually create a new query, 101 00:05:26,300 --> 00:05:29,290 and so this might affect your performance. 102 00:05:29,290 --> 00:05:31,985 Okay, of course if you only do it once or twice 103 00:05:31,985 --> 00:05:35,430 and in a kind of small application, then that small 104 00:05:35,430 --> 00:05:38,170 hit on performance is no big deal at all. 105 00:05:38,170 --> 00:05:40,944 But in a huge application, with tons of populates 106 00:05:40,944 --> 00:05:43,469 all over the place, then that might indeed 107 00:05:43,469 --> 00:05:46,450 have some kind of effect, okay? 108 00:05:46,450 --> 00:05:47,960 So just keep that in mind. 109 00:05:47,960 --> 00:05:51,430 Okay, and really it makes sense, right 110 00:05:51,430 --> 00:05:54,700 because how else would Mongoose be able to get data 111 00:05:54,700 --> 00:05:57,700 about tours and users at the same time. 112 00:05:57,700 --> 00:06:01,050 It needs to create a new query basically in order to 113 00:06:01,050 --> 00:06:03,960 be able to create this connection. 114 00:06:03,960 --> 00:06:06,060 Okay and now just to finish this, 115 00:06:06,060 --> 00:06:07,763 let's remember what I showed you that 116 00:06:07,763 --> 00:06:11,610 this actually didn't work when we get all the tours. 117 00:06:11,610 --> 00:06:15,460 So, remember how in this situation, 118 00:06:15,460 --> 00:06:18,620 we still simply get the ids of the tour guides 119 00:06:18,620 --> 00:06:20,850 and not the referenced user data. 120 00:06:20,850 --> 00:06:24,050 Okay, and so one solution would basically 121 00:06:24,050 --> 00:06:27,956 be to copy this code here, so this populate function, 122 00:06:27,956 --> 00:06:30,950 also here into this route handler, 123 00:06:30,950 --> 00:06:34,390 but of course, duplicate code is never a good idea. 124 00:06:34,390 --> 00:06:35,635 And I hope that you already know 125 00:06:35,635 --> 00:06:39,640 at this point of a better way of doing this. 126 00:06:39,640 --> 00:06:42,310 And the answer to that is query middleware. 127 00:06:42,310 --> 00:06:46,833 Okay, so let's quickly go ahead and move to our model, 128 00:06:47,910 --> 00:06:49,620 and I'm copying this code here now 129 00:06:51,731 --> 00:06:55,253 and so, yeah let's do that right here. 130 00:06:57,580 --> 00:06:58,413 So, 131 00:06:59,430 --> 00:07:03,460 a pre and then I will actually just as before 132 00:07:03,460 --> 00:07:06,640 do it with a regular expression, which is then gonna work 133 00:07:06,640 --> 00:07:09,363 for everything that starts with find. 134 00:07:11,830 --> 00:07:13,083 So, just like this, 135 00:07:17,010 --> 00:07:21,500 Okay, and of course, we do this in query middleware, 136 00:07:21,500 --> 00:07:23,820 well because, this is the kind of middleware 137 00:07:23,820 --> 00:07:27,000 that is going to run each time there is a query. 138 00:07:27,000 --> 00:07:30,540 Right, let's just put all of these pre middlewares together 139 00:07:32,180 --> 00:07:34,943 and then after that have the post one. 140 00:07:37,230 --> 00:07:42,230 All right, so let me copy the code that we had before 141 00:07:42,660 --> 00:07:45,410 and now let's just add it to this, 142 00:07:45,410 --> 00:07:47,620 because remember that in query middleware, 143 00:07:47,620 --> 00:07:50,810 this always points to the current query. 144 00:07:50,810 --> 00:07:53,760 And so now basically all of the queries will then 145 00:07:53,760 --> 00:07:56,560 automatically populate the guides field 146 00:07:56,560 --> 00:07:58,740 with the referenced user. 147 00:07:58,740 --> 00:08:02,053 Okay, and so we can now get rid of it here. 148 00:08:05,040 --> 00:08:07,770 And yeah, that's actually it. 149 00:08:07,770 --> 00:08:09,720 So now we do it here, instead of doing it 150 00:08:09,720 --> 00:08:12,040 in two places, in the controller. 151 00:08:12,040 --> 00:08:15,200 And so this is a nice little trick in case that you always 152 00:08:15,200 --> 00:08:17,713 want to populate all your documents. 153 00:08:19,130 --> 00:08:20,923 So, let's test it out. 154 00:08:21,940 --> 00:08:23,993 First, if it still works here. 155 00:08:24,840 --> 00:08:27,553 And indeed it does, so we get our two users. 156 00:08:28,530 --> 00:08:32,120 And then let's test it here where it didn't work before 157 00:08:32,120 --> 00:08:36,060 and now it does, so here's the result of that. 158 00:08:36,060 --> 00:08:38,790 And now just to make sure that it does actually work, 159 00:08:38,790 --> 00:08:43,384 let's also update it and basically add another tour guide. 160 00:08:43,384 --> 00:08:48,320 So let's copy the id of the tour here then update tour 161 00:08:49,300 --> 00:08:51,160 so we need to put the id here, 162 00:08:51,160 --> 00:08:55,563 and then in the body let's get it from create tour, 163 00:08:58,140 --> 00:08:59,433 so this guides, 164 00:09:02,040 --> 00:09:04,360 and put that here, and then we also 165 00:09:04,360 --> 00:09:07,410 need to get our other user. 166 00:09:07,410 --> 00:09:10,960 So, it's this one here, which is actually not a guide 167 00:09:10,960 --> 00:09:14,710 or not even an admin, but still this is just for testing, 168 00:09:14,710 --> 00:09:16,933 and so let's grab this id, 169 00:09:19,450 --> 00:09:22,423 and then go ahead and put it here. 170 00:09:24,090 --> 00:09:28,640 So, let's update it, that still works, 171 00:09:28,640 --> 00:09:31,070 and so now actually, even in this output, 172 00:09:31,070 --> 00:09:34,130 we already have the populated data. 173 00:09:34,130 --> 00:09:36,410 Okay, because in our regular expression 174 00:09:36,410 --> 00:09:38,300 remember we used find. 175 00:09:38,300 --> 00:09:42,450 And so, this updating here is implemented using find 176 00:09:42,450 --> 00:09:45,465 by id and update, and so it also starts with find 177 00:09:45,465 --> 00:09:49,110 and so therefore the query middleware that we were before 178 00:09:49,110 --> 00:09:51,690 also applies to this function. 179 00:09:51,690 --> 00:09:55,440 Okay and so, this is how populating works. 180 00:09:55,440 --> 00:09:58,640 Okay, so again, extremely important tool 181 00:09:58,640 --> 00:10:01,200 in your Mongoose toolbox, all right. 182 00:10:01,200 --> 00:10:02,910 And just to make sure, let's take 183 00:10:02,910 --> 00:10:05,770 15 seconds here to recap. 184 00:10:05,770 --> 00:10:08,700 So this is a two step process. 185 00:10:08,700 --> 00:10:12,080 First, you create a reference to another model. 186 00:10:12,080 --> 00:10:14,680 And so, with this, you effectively create 187 00:10:14,680 --> 00:10:17,410 the relationship between these two datasets. 188 00:10:17,410 --> 00:10:20,349 Then, in the second step, you populate that field 189 00:10:20,349 --> 00:10:23,010 that you just specified before, 190 00:10:23,010 --> 00:10:26,920 so guides using the populate method. 191 00:10:26,920 --> 00:10:30,350 All right, so very simple, very straightforward, 192 00:10:30,350 --> 00:10:32,330 all you need is to really understand 193 00:10:32,330 --> 00:10:34,620 the logic behind all of this and then 194 00:10:34,620 --> 00:10:37,163 you're ready to actually start using it.