1 00:00:01,090 --> 00:00:03,630 Let's now implement result sorting. 2 00:00:03,630 --> 00:00:07,000 In order to enable our users to sort result 3 00:00:07,000 --> 00:00:08,763 based on a certain field. 4 00:00:10,180 --> 00:00:14,033 So, let's now take a look at all our results here for now. 5 00:00:14,970 --> 00:00:17,930 All right, and so let's say that we actually want 6 00:00:17,930 --> 00:00:21,129 to sort these results, let's say by price. 7 00:00:21,129 --> 00:00:24,560 Okay, so starting with the lowest price and then moving up 8 00:00:24,560 --> 00:00:27,300 all the way to the highest price. 9 00:00:27,300 --> 00:00:29,462 All right, so we have all kinds of prices. 10 00:00:29,462 --> 00:00:33,830 397, 497, 1197, 1497, 997. 11 00:00:36,330 --> 00:00:38,950 We have even almost 3,000 here. 12 00:00:38,950 --> 00:00:42,570 Then 1497 and so they're not really in order, 13 00:00:42,570 --> 00:00:47,450 and so let's allow the user to basically sort these results 14 00:00:47,450 --> 00:00:50,963 based on a field that can be passed using the query string. 15 00:00:53,690 --> 00:00:58,663 So let's say we want to sort by price. 16 00:00:59,860 --> 00:01:00,890 All right. 17 00:01:00,890 --> 00:01:03,480 So if we run this now, of course, nothing will happen, 18 00:01:03,480 --> 00:01:05,860 it will still show the exact same nine results 19 00:01:05,860 --> 00:01:09,900 and in the same order, because before in the last lecture, 20 00:01:09,900 --> 00:01:13,710 we filtered out basically this sort field. 21 00:01:13,710 --> 00:01:14,543 Right? 22 00:01:14,543 --> 00:01:16,883 And so let's now actually go ahead and use it. 23 00:01:18,020 --> 00:01:19,120 All right? 24 00:01:19,120 --> 00:01:22,650 So, remember here how we created the query object 25 00:01:22,650 --> 00:01:25,430 and then excluded the sort field here, 26 00:01:25,430 --> 00:01:28,840 so that it didn't pollute our filtering, right? 27 00:01:28,840 --> 00:01:33,073 But now we actually need it and so let's now use it. 28 00:01:34,150 --> 00:01:38,040 So, the third feature here is sorting. 29 00:01:38,040 --> 00:01:39,790 Or actually, well that's the second one, 30 00:01:39,790 --> 00:01:43,977 so let's just call this one 1A and 1B. 31 00:01:43,977 --> 00:01:44,963 All right? 32 00:01:46,400 --> 00:01:51,400 So, let's say that if there actually is a sort, 33 00:01:53,750 --> 00:01:56,980 sort req.query.sort. 34 00:01:56,980 --> 00:01:58,913 Okay, and you see it down here. 35 00:01:59,750 --> 00:02:04,210 Now it's gone because of this error here, but anyway. 36 00:02:04,210 --> 00:02:06,427 From our last request we got, of course, 37 00:02:06,427 --> 00:02:09,290 there's a query object here and so in there 38 00:02:09,290 --> 00:02:12,840 we had the sort property, all right? 39 00:02:12,840 --> 00:02:15,010 And if there is a sort property, 40 00:02:15,010 --> 00:02:16,780 well, that means that then 41 00:02:16,780 --> 00:02:20,935 we want to actually sort the results based on the value. 42 00:02:20,935 --> 00:02:23,400 Now one quick thing that we need to do, 43 00:02:23,400 --> 00:02:26,040 is to change this query of variable here from 44 00:02:26,040 --> 00:02:29,630 a constant to a normal variable. 45 00:02:29,630 --> 00:02:31,420 So let, okay? 46 00:02:31,420 --> 00:02:34,284 'Cause now we actually want to chain something 47 00:02:34,284 --> 00:02:36,000 to this query. 48 00:02:36,000 --> 00:02:39,600 So, the new query will be query.sort. 49 00:02:42,410 --> 00:02:46,580 And we want to sort it by req.query.sort. 50 00:02:48,607 --> 00:02:52,060 All right, 'cause that will be the value of the field, 51 00:02:52,060 --> 00:02:53,760 so in this case price, 52 00:02:53,760 --> 00:02:56,270 and Mongoose will then automatically sort the result 53 00:02:56,270 --> 00:02:57,910 based on the price. 54 00:02:57,910 --> 00:02:59,640 And now in case you're wondering, 55 00:02:59,640 --> 00:03:02,600 why it works this way, so why we did this here, 56 00:03:02,600 --> 00:03:05,910 just remember what we talked about over the last lectures. 57 00:03:05,910 --> 00:03:08,910 So, remember how this tour.find here 58 00:03:08,910 --> 00:03:11,030 is gonna return a query, right? 59 00:03:11,030 --> 00:03:13,320 And so we stored that query object here 60 00:03:13,320 --> 00:03:15,320 in this variable, so that later on 61 00:03:15,320 --> 00:03:17,800 we can keep chaining more methods to it. 62 00:03:17,800 --> 00:03:19,990 So more of these methods that are available 63 00:03:19,990 --> 00:03:23,150 on all documents created through the query class. 64 00:03:23,150 --> 00:03:24,780 Okay, remember that? 65 00:03:24,780 --> 00:03:27,620 I think we talked about that in depth two lectures ago. 66 00:03:27,620 --> 00:03:31,180 So in case this is still a bit confusing to you, 67 00:03:31,180 --> 00:03:32,560 then just go back there 68 00:03:32,560 --> 00:03:36,450 or you can also take a look at the documentation, all right? 69 00:03:36,450 --> 00:03:40,840 Now let's actually test this out. 70 00:03:40,840 --> 00:03:43,500 So sort is still set to price. 71 00:03:43,500 --> 00:03:45,402 So I'll send it now. 72 00:03:45,402 --> 00:03:48,210 Let's take a look at the result. 73 00:03:48,210 --> 00:03:50,250 And indeed we start with the lowest price, 74 00:03:50,250 --> 00:03:52,270 but I think we already had that before. 75 00:03:52,270 --> 00:03:57,270 So we start with 397, then 497, then 997, 1197, 76 00:03:59,310 --> 00:04:04,020 1497, and you see that the prices actually keep increasing. 77 00:04:04,020 --> 00:04:07,523 So 1997, 2997. 78 00:04:08,570 --> 00:04:11,163 And so yeah, indeed, they are now sorted. 79 00:04:12,830 --> 00:04:13,663 Okay? 80 00:04:13,663 --> 00:04:16,610 So we sorted them by the price in an ascending order. 81 00:04:16,610 --> 00:04:19,430 But we can also sort them in a descending order. 82 00:04:19,430 --> 00:04:20,810 That's very simple. 83 00:04:20,810 --> 00:04:24,090 All we have to do is to set as the minus here. 84 00:04:24,090 --> 00:04:27,200 So, minus price and Mongoose will then automatically 85 00:04:27,200 --> 00:04:29,040 sort them in the other order. 86 00:04:29,040 --> 00:04:31,221 So, in the descending order. 87 00:04:31,221 --> 00:04:34,220 So let's test out that one as well. 88 00:04:34,220 --> 00:04:36,750 And so now we should start with, yeah exactly, 89 00:04:36,750 --> 00:04:38,150 with this one. 90 00:04:38,150 --> 00:04:40,190 So, the first one is now the highest 91 00:04:40,190 --> 00:04:43,960 and the last one is the 397 one. 92 00:04:43,960 --> 00:04:44,930 Cool. 93 00:04:44,930 --> 00:04:48,030 But we can actually take it one step further, 94 00:04:48,030 --> 00:04:51,500 because we have many cases here which have the same price. 95 00:04:51,500 --> 00:04:54,390 But how are these results with the same price then 96 00:04:54,390 --> 00:04:56,190 ordered within them? 97 00:04:56,190 --> 00:04:58,468 So let's say we have this one with this price 98 00:04:58,468 --> 00:05:00,700 and then this one with the same price. 99 00:05:00,700 --> 00:05:05,330 So why does this one appear before the second one? 100 00:05:05,330 --> 00:05:08,050 The same thing with this one here maybe, 101 00:05:08,050 --> 00:05:09,720 oh actually not. 102 00:05:09,720 --> 00:05:12,496 So this one is just the only one with 1997. 103 00:05:12,496 --> 00:05:16,650 But then here again we have these two with the same price. 104 00:05:16,650 --> 00:05:21,563 We have, okay, they are all different. 105 00:05:23,360 --> 00:05:25,150 Okay, these are all different. 106 00:05:25,150 --> 00:05:27,610 But we want to rank them, basically, 107 00:05:27,610 --> 00:05:29,900 according to a second criteria. 108 00:05:29,900 --> 00:05:31,790 So in case there is a tie, 109 00:05:31,790 --> 00:05:33,570 then we want to have a second field 110 00:05:33,570 --> 00:05:36,550 by which we can then sort where the first one is the same. 111 00:05:36,550 --> 00:05:38,230 So in this case the price. 112 00:05:38,230 --> 00:05:40,500 So in Mongoose that's quite easy, 113 00:05:40,500 --> 00:05:43,400 and let me show that to you here as a comment for example. 114 00:05:44,450 --> 00:05:47,510 So in Mongoose we would just say sort 115 00:05:49,650 --> 00:05:52,980 and then right now we have the price, 116 00:05:52,980 --> 00:05:54,430 so we have it just like this, 117 00:05:54,430 --> 00:05:56,340 but then in Mongoose all we have to do 118 00:05:56,340 --> 00:05:59,690 is to add another field name to the string. 119 00:05:59,690 --> 00:06:02,563 So for example, rating average. 120 00:06:03,451 --> 00:06:06,130 Or I think it's ratings. 121 00:06:06,130 --> 00:06:10,460 Okay, and so we now want to pass this field here as well 122 00:06:10,460 --> 00:06:11,363 to our sort. 123 00:06:12,640 --> 00:06:13,473 Okay? 124 00:06:13,473 --> 00:06:15,900 Now we cannot leave a space here in the URL 125 00:06:15,900 --> 00:06:17,933 and so instead we're gonna add a comma. 126 00:06:18,880 --> 00:06:19,713 Okay? 127 00:06:19,713 --> 00:06:22,080 So we want to sort first by price 128 00:06:22,080 --> 00:06:25,862 and then as a second criteria, also by ratings average. 129 00:06:25,862 --> 00:06:26,757 Okay? 130 00:06:26,757 --> 00:06:28,440 And so now all we need to do 131 00:06:28,440 --> 00:06:33,060 is to basically replace this comma here with a space. 132 00:06:33,060 --> 00:06:33,893 All right? 133 00:06:33,893 --> 00:06:36,004 And of course, we could have even more fields 134 00:06:36,004 --> 00:06:38,460 by which we sort. 135 00:06:38,460 --> 00:06:39,293 Okay? 136 00:06:39,293 --> 00:06:40,900 But for now let's just leave it like this 137 00:06:40,900 --> 00:06:43,813 and implement it very quickly in our code. 138 00:06:44,820 --> 00:06:45,810 All right? 139 00:06:45,810 --> 00:06:48,543 And actually let me create a new variable here. 140 00:06:49,870 --> 00:06:51,303 Let's call it sort by. 141 00:06:53,014 --> 00:06:57,410 And so we have req.query.sort 142 00:06:57,410 --> 00:07:01,020 and what we're gonna do is to now split it by the comma. 143 00:07:01,020 --> 00:07:05,867 So split the string by the comma, 144 00:07:05,867 --> 00:07:08,220 and so this will then return an array of all the strings, 145 00:07:08,220 --> 00:07:09,720 so all the field names, 146 00:07:09,720 --> 00:07:13,060 and then all we have to do is to put it back together 147 00:07:13,060 --> 00:07:17,453 using join, and as the join string we use a space. 148 00:07:18,490 --> 00:07:19,980 All right? 149 00:07:19,980 --> 00:07:23,223 And so just to make sure that it's correct. 150 00:07:24,280 --> 00:07:29,280 Console.log, sort by, and then of course here 151 00:07:29,363 --> 00:07:32,423 we want to use that sort by variable. 152 00:07:34,280 --> 00:07:35,240 Okay? 153 00:07:35,240 --> 00:07:36,690 So let's take a look at that. 154 00:07:40,450 --> 00:07:41,633 And. 155 00:07:43,350 --> 00:07:48,350 So 4.7 here, and the other one also has 4.7, all right? 156 00:07:48,990 --> 00:07:52,130 And so let's actually go ahead and use our API 157 00:07:52,130 --> 00:07:54,360 to change this ratings average here. 158 00:07:54,360 --> 00:07:55,193 Okay? 159 00:07:55,193 --> 00:07:56,740 So just as a fun experiment, 160 00:07:56,740 --> 00:08:00,483 let's copy this ID here, then update tour, 161 00:08:02,060 --> 00:08:04,440 and so let's come here to the body, 162 00:08:04,440 --> 00:08:08,367 and so I want to update ratings average to 4.9. 163 00:08:13,465 --> 00:08:14,380 Okay? 164 00:08:14,380 --> 00:08:18,513 Let's send that, and indeed, you see that it got updated. 165 00:08:21,120 --> 00:08:22,940 And so now let's change that again, 166 00:08:22,940 --> 00:08:24,700 or let's try that again actually. 167 00:08:24,700 --> 00:08:27,534 And so now you see the first one still has 4.7 168 00:08:27,534 --> 00:08:30,510 and this one here now has 4.9. 169 00:08:30,510 --> 00:08:34,476 And so they are now ordered with an ascending rating. 170 00:08:34,476 --> 00:08:35,600 Right? 171 00:08:35,600 --> 00:08:38,335 And of course, we could put a minus here 172 00:08:38,335 --> 00:08:41,653 and so then they should change positions, 173 00:08:41,653 --> 00:08:45,970 meaning that the one with 4.9 should appear first. 174 00:08:45,970 --> 00:08:47,960 Let's try that as well. 175 00:08:47,960 --> 00:08:50,590 And yeah, indeed. 176 00:08:50,590 --> 00:08:53,010 So now this one, the sports lover tour, 177 00:08:53,010 --> 00:08:55,230 is the one that appears first. 178 00:08:55,230 --> 00:08:56,710 All right, great. 179 00:08:56,710 --> 00:08:58,800 So it looks like it's working. 180 00:08:58,800 --> 00:09:01,270 Let's a take a look at our results here. 181 00:09:01,270 --> 00:09:02,670 And yeah, indeed. 182 00:09:02,670 --> 00:09:04,970 And let's just take a look at what we logged here 183 00:09:04,970 --> 00:09:06,190 at the console. 184 00:09:06,190 --> 00:09:10,453 And so, yeah, this is our sort by variable, which yeah. 185 00:09:11,790 --> 00:09:14,718 Which kind of looks similar to what we have here. 186 00:09:14,718 --> 00:09:17,560 And so that was the goal of creating this variable. 187 00:09:17,560 --> 00:09:19,410 Great, so that's now working. 188 00:09:19,410 --> 00:09:23,263 Let's get rid of this console and this one here as well. 189 00:09:24,430 --> 00:09:26,180 And all right. 190 00:09:26,180 --> 00:09:27,140 And just to finish here, 191 00:09:27,140 --> 00:09:29,910 let's actually add a default one. 192 00:09:29,910 --> 00:09:33,565 And we do that by simply adding an else block here. 193 00:09:33,565 --> 00:09:36,580 So in case that the user does not specify 194 00:09:36,580 --> 00:09:39,770 any sort field in the URL query string, 195 00:09:39,770 --> 00:09:42,583 we're still gonna add a sort to the query. 196 00:09:43,850 --> 00:09:47,300 So query.sort and we will then sort 197 00:09:47,300 --> 00:09:49,990 by the created add field, all right? 198 00:09:49,990 --> 00:09:51,830 And actually in a descending order, 199 00:09:51,830 --> 00:09:54,330 so that the newest ones appear first. 200 00:09:54,330 --> 00:09:59,070 So minus created at. 201 00:09:59,070 --> 00:10:00,050 All right? 202 00:10:00,050 --> 00:10:01,730 Give this a save. 203 00:10:01,730 --> 00:10:05,240 And so yeah, I think like this, we're done. 204 00:10:05,240 --> 00:10:09,140 Let's just turn off this option here as well. 205 00:10:09,140 --> 00:10:12,503 And so now we just are back to our regular URL 206 00:10:13,390 --> 00:10:15,100 without any query strings. 207 00:10:15,100 --> 00:10:16,570 And so let's take a look 208 00:10:16,570 --> 00:10:19,780 if this is now actually ordered by the created at. 209 00:10:19,780 --> 00:10:22,713 So this one here ends in 59 seconds and 30. 210 00:10:23,730 --> 00:10:25,230 5930 as well. 211 00:10:25,230 --> 00:10:27,363 And yeah. 212 00:10:27,363 --> 00:10:29,670 So they were all created at the exact same moment, 213 00:10:29,670 --> 00:10:32,990 because we imported them all at the same time. 214 00:10:32,990 --> 00:10:33,823 All right? 215 00:10:33,823 --> 00:10:36,250 And so in this case, we cannot really see 216 00:10:36,250 --> 00:10:38,920 the effect of this piece of code 217 00:10:38,920 --> 00:10:41,810 that we just added here, but in case these documents 218 00:10:41,810 --> 00:10:44,130 were added at different times, then they would now 219 00:10:44,130 --> 00:10:46,901 be ordered by the date they were created. 220 00:10:46,901 --> 00:10:48,430 All right? 221 00:10:48,430 --> 00:10:50,450 So that's it for sorting. 222 00:10:50,450 --> 00:10:53,320 Next up, we're gonna talk about limiting the fields 223 00:10:53,320 --> 00:10:54,763 that we want in our results.