All language subtitles for 014 Making the API Better_ Filtering_Downloadly.ir_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,210 --> 00:00:03,670 Let's now keep working on our API. 2 00:00:04,530 --> 00:00:06,620 Over the next videos, we will implement 3 00:00:06,620 --> 00:00:08,840 a couple of common API features 4 00:00:08,840 --> 00:00:12,440 that make an API easier and more pleasant to use 5 00:00:12,440 --> 00:00:14,950 for whomever is gonna use it. 6 00:00:14,950 --> 00:00:15,940 And in this lecture, 7 00:00:15,940 --> 00:00:17,963 we're gonna start with filtering. 8 00:00:19,340 --> 00:00:22,830 So again, the first feature that we're gonna implement 9 00:00:22,830 --> 00:00:26,000 is to allow the user to basically filter data 10 00:00:26,000 --> 00:00:27,563 using a query string. 11 00:00:28,890 --> 00:00:32,232 Let me just tell you what a query string is using Postman. 12 00:00:36,610 --> 00:00:39,993 So, let's close all these guys. 13 00:00:46,150 --> 00:00:47,683 Alright. 14 00:00:47,683 --> 00:00:50,350 Actually where filtering is gonna make sense 15 00:00:50,350 --> 00:00:52,793 is here on this get all tours route. 16 00:00:53,750 --> 00:00:55,940 Because, this is the one that, 17 00:00:55,940 --> 00:00:58,720 well as the name says, gets all the tours. 18 00:00:58,720 --> 00:01:02,370 And so here, we want to allow the user to filter the data, 19 00:01:02,370 --> 00:01:04,840 so that instead of getting all the data, 20 00:01:04,840 --> 00:01:08,123 he only gets the data that matches the filter. 21 00:01:09,340 --> 00:01:12,290 Like this, we can basically allow the user 22 00:01:12,290 --> 00:01:14,750 to query the data in a very easy way. 23 00:01:14,750 --> 00:01:16,480 Again, using the query string. 24 00:01:16,480 --> 00:01:18,850 And the query string looks a bit like this. 25 00:01:18,850 --> 00:01:20,340 And I'm sure you've seen it. 26 00:01:20,340 --> 00:01:23,120 So we start with a question mark, 27 00:01:23,120 --> 00:01:26,663 and then we can simply specify some field value pairs. 28 00:01:28,420 --> 00:01:30,360 So let's say we wanted to filter 29 00:01:30,360 --> 00:01:33,620 for a duration equal to five and 30 00:01:33,620 --> 00:01:36,573 at the same time, a difficulty of easy. 31 00:01:40,500 --> 00:01:44,690 So we can very simply build a query string like this. 32 00:01:44,690 --> 00:01:46,860 So again, it starts with this question mark, 33 00:01:46,860 --> 00:01:48,800 and then we can specify as many 34 00:01:48,800 --> 00:01:51,723 key value pairs using this format. 35 00:01:53,000 --> 00:01:54,860 And now our Postman application here 36 00:01:54,860 --> 00:01:57,450 actually recognizes this and basically 37 00:01:57,450 --> 00:02:00,420 parses this string here and puts the key and 38 00:02:00,420 --> 00:02:04,140 the values down here in this Params tab. 39 00:02:04,140 --> 00:02:06,963 So we could also specify the key and values here. 40 00:02:07,890 --> 00:02:11,110 So lets say test, set it to twenty three, and 41 00:02:11,110 --> 00:02:12,690 so you see up here that it actually 42 00:02:12,690 --> 00:02:14,483 added it to the query string. 43 00:02:15,370 --> 00:02:18,740 So that's a very nice way of Postman helping us here. 44 00:02:18,740 --> 00:02:21,150 Because now we can, for example, while we're 45 00:02:21,150 --> 00:02:24,260 doing testing we can turn these on and off 46 00:02:24,260 --> 00:02:25,610 and you see that the query string 47 00:02:25,610 --> 00:02:27,573 in here reacts accordingly. 48 00:02:28,970 --> 00:02:32,940 Anyway, we now need a way of accessing this data 49 00:02:32,940 --> 00:02:35,883 that's in a query string, in our Express application. 50 00:02:36,730 --> 00:02:39,210 But, luckily for us, that's very easy, 51 00:02:39,210 --> 00:02:42,260 because Express already took care of that. 52 00:02:42,260 --> 00:02:44,690 So that's just one of the many many things 53 00:02:44,690 --> 00:02:48,420 that Express does for us in order to really make 54 00:02:48,420 --> 00:02:50,563 NodeJS development a lot faster. 55 00:02:52,420 --> 00:02:55,170 So let me show it to you here, actually in the console. 56 00:02:57,810 --> 00:03:01,380 So this data is on a request, which makes sense, 57 00:03:01,380 --> 00:03:04,013 and then it is in a field called query. 58 00:03:04,980 --> 00:03:07,490 So request dot query should then give us 59 00:03:07,490 --> 00:03:10,630 an object nicely formatted with the data 60 00:03:10,630 --> 00:03:11,730 from the query string. 61 00:03:12,760 --> 00:03:13,593 Give it a save. 62 00:03:14,530 --> 00:03:16,630 And note, how I actually added it to 63 00:03:16,630 --> 00:03:20,150 the get all tours handler function. 64 00:03:20,150 --> 00:03:22,800 So again, we do this kind of filtering 65 00:03:22,800 --> 00:03:25,323 in the route where we want to get all the tours. 66 00:03:27,140 --> 00:03:30,373 Let's test it out now with this exact query string. 67 00:03:31,400 --> 00:03:32,920 Sent the request. 68 00:03:32,920 --> 00:03:36,333 And coming back, here we got the object. 69 00:03:37,170 --> 00:03:40,420 Let's actually make this here bigger 70 00:03:40,420 --> 00:03:41,720 so you can see. 71 00:03:41,720 --> 00:03:43,650 That's not really working. 72 00:03:43,650 --> 00:03:45,820 So sometimes I know some people cannot see 73 00:03:45,820 --> 00:03:47,630 the bottom of the screen. 74 00:03:47,630 --> 00:03:49,080 Let me just do it again here. 75 00:03:51,280 --> 00:03:52,942 And so, here we go. 76 00:03:52,942 --> 00:03:55,990 We have the object, which has the duration set to five 77 00:03:55,990 --> 00:03:58,200 and the difficulty set to easy. 78 00:03:58,200 --> 00:04:00,460 And so you see that Express, very nicely, 79 00:04:00,460 --> 00:04:04,250 parsed that string into this, easy to use, object. 80 00:04:04,250 --> 00:04:06,330 Now one thing that I noticed here is that 81 00:04:06,330 --> 00:04:08,380 we're actually missing the log that we had 82 00:04:08,380 --> 00:04:10,440 easier here in the console. 83 00:04:10,440 --> 00:04:12,540 Let's take a look at why that's happening. 84 00:04:15,431 --> 00:04:18,430 So for some reason, we now have the application 85 00:04:18,430 --> 00:04:21,310 before we actually read the environment variable. 86 00:04:21,310 --> 00:04:24,113 That way, the application does not get access to them. 87 00:04:25,010 --> 00:04:27,600 I thought that I had fixed this before, 88 00:04:27,600 --> 00:04:29,300 but for some reason it's back now. 89 00:04:30,340 --> 00:04:32,390 Give it another save here. 90 00:04:32,390 --> 00:04:33,690 And I will try that again. 91 00:04:36,180 --> 00:04:38,933 Now we get our logging back. 92 00:04:40,200 --> 00:04:43,750 A get request to this URL and then with 93 00:04:43,750 --> 00:04:45,963 this rest of the data here. 94 00:04:48,260 --> 00:04:50,600 Anyway, let's now actually use that data 95 00:04:50,600 --> 00:04:53,200 in order to implement our filtering. 96 00:04:53,200 --> 00:04:55,840 But before we can do that, I need to tell you that 97 00:04:55,840 --> 00:04:58,440 in Mongoose, there are actually two ways of 98 00:04:58,440 --> 00:05:00,120 writing database queries. 99 00:05:00,120 --> 00:05:03,050 The first one is to just use filter object 100 00:05:03,050 --> 00:05:06,490 just like we did in the MongoDB introduction section. 101 00:05:06,490 --> 00:05:08,690 Right? Remember that? 102 00:05:08,690 --> 00:05:12,333 The second way is to use some special Mongoose methods. 103 00:05:13,590 --> 00:05:16,505 Let me just, very quickly, show that to you. 104 00:05:16,505 --> 00:05:19,420 With the filter object, we would do it like this. 105 00:05:19,420 --> 00:05:21,910 We start with the find here, and then in there 106 00:05:21,910 --> 00:05:23,570 we can pass a filter object. 107 00:05:23,570 --> 00:05:25,900 And I already mentioned that before. 108 00:05:25,900 --> 00:05:28,410 Again, it works the exact same way as 109 00:05:28,410 --> 00:05:30,650 with a normal MongoDB query. 110 00:05:30,650 --> 00:05:33,160 We could now say, duration 111 00:05:34,570 --> 00:05:35,657 set it to five. 112 00:05:35,657 --> 00:05:36,663 And then, 113 00:05:39,320 --> 00:05:42,253 difficulty, set it to easy. 114 00:05:44,300 --> 00:05:47,150 That's exactly the query string we had before, 115 00:05:47,150 --> 00:05:48,960 and now I'm hard coding it here just to 116 00:05:48,960 --> 00:05:50,860 show you the two different ways of 117 00:05:50,860 --> 00:05:52,423 writing queries in Mongoose. 118 00:05:53,318 --> 00:05:54,518 Let's test this one out. 119 00:05:58,220 --> 00:06:00,453 You see that now, we only get two results. 120 00:06:01,290 --> 00:06:03,193 Let's give ourselves some space here. 121 00:06:04,810 --> 00:06:05,760 Alright. 122 00:06:05,760 --> 00:06:08,870 We see the two results and indeed 123 00:06:08,870 --> 00:06:10,890 they both have a duration of five, 124 00:06:10,890 --> 00:06:15,890 so here and here, and they both are easy. 125 00:06:15,930 --> 00:06:17,443 Easy and easy. 126 00:06:18,350 --> 00:06:20,563 So our filter worked just fine. 127 00:06:21,675 --> 00:06:24,710 That is the first way of writing a query. 128 00:06:24,710 --> 00:06:27,650 Just the normal way that we're already used to, 129 00:06:27,650 --> 00:06:29,103 but there is another way. 130 00:06:30,790 --> 00:06:32,993 Let me show that to you as well. 131 00:06:37,570 --> 00:06:39,910 Again, we start in the exact same way, 132 00:06:39,910 --> 00:06:41,690 but then the difference is that we start 133 00:06:41,690 --> 00:06:44,150 chaining some special Mongoose method to 134 00:06:44,150 --> 00:06:47,523 basically build the query similar to this one that we have. 135 00:06:49,540 --> 00:06:50,563 So we say, 136 00:06:51,730 --> 00:06:52,563 where 137 00:06:54,910 --> 00:06:55,780 the duration 138 00:06:58,840 --> 00:07:00,343 equals five. 139 00:07:01,810 --> 00:07:03,630 Then also, where 140 00:07:04,710 --> 00:07:06,180 the difficulty, 141 00:07:06,180 --> 00:07:08,023 that needs to be in quotes, 142 00:07:11,420 --> 00:07:12,253 equals 143 00:07:13,170 --> 00:07:14,003 easy. 144 00:07:17,270 --> 00:07:19,620 This query here is gonna be exactly 145 00:07:19,620 --> 00:07:22,500 the same as this one, but again, written using 146 00:07:22,500 --> 00:07:24,740 special Mongoose methods. 147 00:07:24,740 --> 00:07:27,240 And there are of course tons of other methods. 148 00:07:27,240 --> 00:07:30,980 For example, instead of equals, we can have less than, 149 00:07:30,980 --> 00:07:34,060 or equal, or just less than, or you can also 150 00:07:34,060 --> 00:07:37,520 sort the results or limit the number of results, 151 00:07:37,520 --> 00:07:39,020 and really all kinds of stuff. 152 00:07:41,057 --> 00:07:43,180 For sure, we're gonna use some of them 153 00:07:43,180 --> 00:07:44,743 over the next couple of videos. 154 00:07:46,050 --> 00:07:48,290 This might look a bit weird here to you, 155 00:07:48,290 --> 00:07:51,303 but its actually very, very useful in some cases. 156 00:07:52,530 --> 00:07:55,573 Just to test, give it a try again to this one. 157 00:07:57,010 --> 00:07:59,953 And indeed we get our same two results. 158 00:08:02,735 --> 00:08:04,590 Now, let me comment this one here out. 159 00:08:04,590 --> 00:08:06,950 I'm gonna leave it here just as a reference, 160 00:08:06,950 --> 00:08:10,300 because right now, we're gonna use this method here. 161 00:08:10,300 --> 00:08:12,400 And we're gonna use it, because in fact, 162 00:08:12,400 --> 00:08:15,000 we actually already have an object that 163 00:08:15,000 --> 00:08:16,363 looks a lot like this. 164 00:08:17,360 --> 00:08:20,300 And that is request dot query. 165 00:08:20,300 --> 00:08:22,193 If you compare it, this object, 166 00:08:23,228 --> 00:08:25,128 to this object that you see down here, 167 00:08:26,280 --> 00:08:28,173 looks exactly the same as this one. 168 00:08:30,120 --> 00:08:32,130 To implement a very simple filter, 169 00:08:32,130 --> 00:08:37,130 all we have to do in this case is to say, req dot query. 170 00:08:37,150 --> 00:08:38,793 Let's try that one more time. 171 00:08:39,730 --> 00:08:40,803 Get rid of this here. 172 00:08:42,650 --> 00:08:46,300 And indeed we get the exact same two results. 173 00:08:46,300 --> 00:08:50,223 Now let's just try only with difficulty set to easy. 174 00:08:52,140 --> 00:08:53,993 Now we get four results. 175 00:08:55,280 --> 00:08:56,700 All of them of course will have 176 00:08:56,700 --> 00:08:58,463 the difficulty set to easy. 177 00:09:00,540 --> 00:09:03,100 Our filter, our very basic filter, 178 00:09:03,100 --> 00:09:04,880 is now actually working. 179 00:09:04,880 --> 00:09:06,870 Now, the problem with this implementation, 180 00:09:06,870 --> 00:09:09,650 is that its actually way too simple. 181 00:09:09,650 --> 00:09:11,690 That's because, later on, we will have 182 00:09:11,690 --> 00:09:13,370 other query parameters. 183 00:09:13,370 --> 00:09:16,730 For example, sort, for sorting functionality, 184 00:09:16,730 --> 00:09:18,653 or page, for pagination. 185 00:09:19,610 --> 00:09:21,040 We need to make sure that we are 186 00:09:21,040 --> 00:09:24,180 not querying for these in our database. 187 00:09:24,180 --> 00:09:25,883 For example, if we added here, 188 00:09:27,360 --> 00:09:29,893 page, equal to two, 189 00:09:30,970 --> 00:09:33,993 then we would, of course, not get any result. 190 00:09:35,130 --> 00:09:36,300 Let me show that to you. 191 00:09:36,300 --> 00:09:38,280 And indeed we do not get any result, 192 00:09:38,280 --> 00:09:40,560 because there is no document in this collection 193 00:09:40,560 --> 00:09:42,593 where page is set to two. 194 00:09:43,480 --> 00:09:46,200 We only want to use this parameter here, 195 00:09:46,200 --> 00:09:49,170 or this field, page, to implement pagination 196 00:09:49,170 --> 00:09:51,593 and not to actually query in the database. 197 00:09:53,450 --> 00:09:54,870 So what we will have to do is, 198 00:09:54,870 --> 00:09:57,810 to basically exclude these special field names 199 00:09:57,810 --> 00:10:00,883 from our query string before we actually do the filtering. 200 00:10:02,680 --> 00:10:05,540 Let's now go ahead and implement that. 201 00:10:05,540 --> 00:10:08,200 What we will do is, to first, create a 202 00:10:08,200 --> 00:10:12,333 shallow copy of the request dot query object. 203 00:10:15,690 --> 00:10:17,670 Let's call it query object. 204 00:10:17,670 --> 00:10:20,030 Here we need, really, a hard copy. 205 00:10:20,030 --> 00:10:23,560 We can't just do, request dot query, 206 00:10:23,560 --> 00:10:25,570 because then, if you would delete something 207 00:10:25,570 --> 00:10:28,030 from this object, we would also delete it 208 00:10:28,030 --> 00:10:30,700 from the req dot query object. 209 00:10:30,700 --> 00:10:32,170 And that's because in JavaScript, 210 00:10:32,170 --> 00:10:35,260 when we set a variable to another object, 211 00:10:35,260 --> 00:10:37,490 that new variable will basically just be 212 00:10:37,490 --> 00:10:39,683 a reference to that original object. 213 00:10:41,030 --> 00:10:43,770 So we really need a hard copy here. 214 00:10:43,770 --> 00:10:46,310 In JavaScript, there's not really a built-in way 215 00:10:46,310 --> 00:10:49,020 of doing this, but a very nice trick that we can use, 216 00:10:49,020 --> 00:10:53,520 since ES6, is to use, first, the structuring 217 00:10:53,520 --> 00:10:55,960 which we use by using dot dot dot. 218 00:10:55,960 --> 00:10:58,270 And then we can simply create a 219 00:10:58,270 --> 00:10:59,663 new object out of that. 220 00:11:01,290 --> 00:11:02,123 Just like this. 221 00:11:03,260 --> 00:11:05,420 This structuring here, these three dots, 222 00:11:05,420 --> 00:11:08,683 will basically take all the fields out of the object. 223 00:11:09,860 --> 00:11:11,810 Here with the curly braces, well, 224 00:11:11,810 --> 00:11:13,990 we simply create a new object. 225 00:11:13,990 --> 00:11:15,940 So we have a new object that is basically 226 00:11:15,940 --> 00:11:18,070 going to contain all the key value pairs 227 00:11:18,070 --> 00:11:21,053 that were in our req dot query object. 228 00:11:23,020 --> 00:11:24,330 So that's a copy. 229 00:11:24,330 --> 00:11:26,450 Now let's create an array of 230 00:11:26,450 --> 00:11:28,633 all the fields that we want to exclude. 231 00:11:31,610 --> 00:11:34,040 I'm calling that one excluded fields. 232 00:11:34,040 --> 00:11:37,090 And then we want to exclude page, 233 00:11:37,090 --> 00:11:38,400 so that's the one that I just 234 00:11:38,400 --> 00:11:40,250 told you before in Postman. 235 00:11:40,250 --> 00:11:42,150 Then, we also want to exclude 236 00:11:43,170 --> 00:11:44,440 sort, 237 00:11:44,440 --> 00:11:46,053 we want to exclude limit, 238 00:11:47,890 --> 00:11:50,533 and we want to exclude fields. 239 00:11:52,160 --> 00:11:54,940 We will implement all of the functionality 240 00:11:54,940 --> 00:11:58,210 of paging, sorting, limiting, and selecting 241 00:11:58,210 --> 00:12:01,140 only some specific fields, later 242 00:12:01,140 --> 00:12:02,690 over the next couple of videos. 243 00:12:03,750 --> 00:12:05,900 Next, what we need to do is to 244 00:12:05,900 --> 00:12:08,610 basically remove all of these fields 245 00:12:08,610 --> 00:12:10,230 from our query object. 246 00:12:10,230 --> 00:12:11,690 And in order to do that, we will 247 00:12:11,690 --> 00:12:13,523 loop over these fields. 248 00:12:14,660 --> 00:12:15,860 So, excluded fields 249 00:12:17,690 --> 00:12:19,070 for each. 250 00:12:19,070 --> 00:12:20,390 And we're using for each because 251 00:12:20,390 --> 00:12:22,423 we don't want to save a new array. 252 00:12:24,150 --> 00:12:25,840 Then each of these of elements, 253 00:12:25,840 --> 00:12:27,863 we can use the delete operator. 254 00:12:30,930 --> 00:12:32,530 And then from the query object, 255 00:12:32,530 --> 00:12:34,730 we want to delete the field with 256 00:12:34,730 --> 00:12:36,700 the name of element, so the current 257 00:12:36,700 --> 00:12:38,820 element in the array. 258 00:12:38,820 --> 00:12:42,340 Like this, we will first delete the field of page, 259 00:12:42,340 --> 00:12:45,563 then the sort, then the limit, then the fields. 260 00:12:46,560 --> 00:12:48,440 Of course, only in case if its there. 261 00:12:48,440 --> 00:12:50,860 If there is, for example, no limit field, 262 00:12:50,860 --> 00:12:54,030 then, of course, that will not be deleted, 263 00:12:54,030 --> 00:12:55,130 because its not there. 264 00:12:56,530 --> 00:12:59,233 Let's now take a look if this actually works. 265 00:13:00,380 --> 00:13:02,530 I'm gonna log req dot query and 266 00:13:02,530 --> 00:13:05,723 at the same time our query object. 267 00:13:08,742 --> 00:13:09,692 Giving this a save. 268 00:13:10,600 --> 00:13:12,060 Don't worry about our results now. 269 00:13:12,060 --> 00:13:14,190 All I want to take a look at is 270 00:13:14,190 --> 00:13:17,080 our query object, just to make sure 271 00:13:17,080 --> 00:13:19,530 that this logic that we have here actually works. 272 00:13:21,540 --> 00:13:26,540 Let's add a sort here of one and a limit of 10. 273 00:13:27,260 --> 00:13:29,210 These will make more sense later. 274 00:13:29,210 --> 00:13:30,540 For now, I am just adding them, 275 00:13:30,540 --> 00:13:32,293 so that I can later exclude them. 276 00:13:34,490 --> 00:13:37,120 Results don't matter, all that matters, 277 00:13:37,120 --> 00:13:40,640 is that our original query is this one here, 278 00:13:40,640 --> 00:13:42,820 and then our query object, which is 279 00:13:42,820 --> 00:13:44,010 the one that we're going to use, 280 00:13:44,010 --> 00:13:46,320 only has difficulty set to easy. 281 00:13:46,320 --> 00:13:49,723 That's because we excluded, page, sort, and limit. 282 00:13:51,000 --> 00:13:53,460 This now works, so instead of using 283 00:13:53,460 --> 00:13:57,033 req dot query, we're gonna use our query object. 284 00:13:59,390 --> 00:14:03,000 Let's get rid of that and try it again. 285 00:14:03,000 --> 00:14:05,080 Now we should be back to querying for 286 00:14:05,080 --> 00:14:07,180 all the documents that have a difficulty 287 00:14:07,180 --> 00:14:11,473 set to easy and ignoring all of these other fields. 288 00:14:13,256 --> 00:14:14,150 Send that. 289 00:14:14,150 --> 00:14:15,930 And indeed, we are back to our four 290 00:14:15,930 --> 00:14:18,623 results that we had before. 291 00:14:19,900 --> 00:14:22,610 We can also turn on the duration, 292 00:14:22,610 --> 00:14:24,743 and indeed, we will now have two results. 293 00:14:26,110 --> 00:14:27,180 Remember that? 294 00:14:27,180 --> 00:14:28,643 And if we turn these off, 295 00:14:29,960 --> 00:14:32,380 well, then off course its gonna work the same. 296 00:14:32,380 --> 00:14:34,330 Let's just delete this empty line here. 297 00:14:36,740 --> 00:14:40,340 And so, indeed, with our original query string, 298 00:14:40,340 --> 00:14:42,693 things obviously also still worked the same. 299 00:14:44,220 --> 00:14:47,630 We prepared our filter feature so that it 300 00:14:47,630 --> 00:14:51,410 ignores page, sort, and limit fields. 301 00:14:51,410 --> 00:14:53,450 But there is actually something more we need 302 00:14:53,450 --> 00:14:56,210 to do in our code, in order to basically 303 00:14:56,210 --> 00:14:59,753 be able to implement these features in future videos. 304 00:15:01,360 --> 00:15:03,250 Let's go back here to our code. 305 00:15:03,250 --> 00:15:04,570 So let's now learn a little bit 306 00:15:04,570 --> 00:15:07,093 about how queries actually work in Mongoose. 307 00:15:08,220 --> 00:15:09,910 I think I mentioned this before, 308 00:15:09,910 --> 00:15:13,400 but its very important to talk about this again. 309 00:15:13,400 --> 00:15:16,943 This find method here is going to return a query. 310 00:15:18,060 --> 00:15:20,430 All of this here will return a query. 311 00:15:20,430 --> 00:15:22,410 And that is the reason why we can then 312 00:15:22,410 --> 00:15:24,840 chain other methods like this. 313 00:15:24,840 --> 00:15:28,630 Like where, equals, and all these other methods 314 00:15:28,630 --> 00:15:30,770 that I talked to you about before. 315 00:15:30,770 --> 00:15:33,770 Let me actually show that to you in the documentation again. 316 00:15:37,670 --> 00:15:40,170 Here we have the query object, 317 00:15:40,170 --> 00:15:42,710 and again, when we use the find method, 318 00:15:42,710 --> 00:15:46,590 it will return an object which is a query. 319 00:15:46,590 --> 00:15:48,310 So in query dot prototype, 320 00:15:48,310 --> 00:15:50,180 we then have all of these methods. 321 00:15:50,180 --> 00:15:52,820 For example, down here we have where, 322 00:15:52,820 --> 00:15:54,040 which I just mentioned. 323 00:15:54,040 --> 00:15:55,790 We have sort which is the one 324 00:15:55,790 --> 00:15:58,120 that were gonna use later to sort. 325 00:15:58,120 --> 00:16:00,870 We have, let's see, we have less than 326 00:16:00,870 --> 00:16:03,740 or less than equal, we have greater. 327 00:16:03,740 --> 00:16:05,260 We have... 328 00:16:05,260 --> 00:16:06,700 What else did we use? 329 00:16:06,700 --> 00:16:08,070 We used equals. 330 00:16:08,070 --> 00:16:11,310 All of these are part of query dot prototype. 331 00:16:11,310 --> 00:16:13,920 Which, again, refers to objects that 332 00:16:13,920 --> 00:16:16,783 we're creating using the query class. 333 00:16:19,613 --> 00:16:21,410 Again, this is the reason why 334 00:16:21,410 --> 00:16:24,070 we can do this sort of chaining. 335 00:16:24,070 --> 00:16:26,290 Now comes the important part. 336 00:16:26,290 --> 00:16:28,350 As soon as we actually await the 337 00:16:28,350 --> 00:16:30,970 result of the query, so as soon as 338 00:16:30,970 --> 00:16:33,950 we use await here or here, for example. 339 00:16:33,950 --> 00:16:35,890 As soon as we use this await, 340 00:16:35,890 --> 00:16:37,910 the query will then execute and 341 00:16:37,910 --> 00:16:39,610 come back with the documents that 342 00:16:39,610 --> 00:16:42,144 actually match our query. 343 00:16:42,144 --> 00:16:44,960 If we do it like this, such as we have here, 344 00:16:44,960 --> 00:16:46,730 then there is now way of, later, 345 00:16:46,730 --> 00:16:49,110 implementing sorting, or pagination, 346 00:16:49,110 --> 00:16:50,793 or all of these other features. 347 00:16:51,880 --> 00:16:53,870 Instead, what we will have to do, 348 00:16:53,870 --> 00:16:57,220 is to save this part here into a query, 349 00:16:57,220 --> 00:17:00,200 then in the end, as soon as we change all the methods 350 00:17:00,200 --> 00:17:02,440 to the query that we need to, only then 351 00:17:02,440 --> 00:17:04,863 by the end, we can await that query. 352 00:17:06,079 --> 00:17:08,599 For example, we're going to use the sort method. 353 00:17:08,599 --> 00:17:10,200 We're going to use the predict method, 354 00:17:10,200 --> 00:17:12,440 we're going to use the limit method, 355 00:17:12,440 --> 00:17:14,500 and we're going to use, really, a bunch of methods, 356 00:17:14,500 --> 00:17:16,619 and chain them to this query. 357 00:17:16,619 --> 00:17:18,109 That would be impossible to do, 358 00:17:18,109 --> 00:17:20,910 again, if we await the result of 359 00:17:20,910 --> 00:17:23,073 this initial query here, right away. 360 00:17:24,589 --> 00:17:26,470 I hope that made sense. 361 00:17:26,470 --> 00:17:28,660 The way that we will do it 362 00:17:28,660 --> 00:17:30,140 is that we 363 00:17:31,090 --> 00:17:33,473 will have our tours down here. 364 00:17:35,540 --> 00:17:37,623 And this will await the query. 365 00:17:41,310 --> 00:17:43,830 The query is, well, simply the 366 00:17:43,830 --> 00:17:45,533 query without any await. 367 00:17:47,460 --> 00:17:49,360 Just to make it consistent with 368 00:17:49,360 --> 00:17:50,363 this part here, 369 00:17:52,060 --> 00:17:53,163 let's do it like this. 370 00:17:55,603 --> 00:17:57,123 Let's say that, 371 00:17:58,540 --> 00:18:00,437 first, we build the query 372 00:18:00,437 --> 00:18:03,363 and then we execute the query. 373 00:18:07,290 --> 00:18:08,313 That's not correct. 374 00:18:11,490 --> 00:18:13,040 Since we're doing comments here, 375 00:18:13,040 --> 00:18:14,330 let's also say send 376 00:18:15,300 --> 00:18:16,790 response. 377 00:18:16,790 --> 00:18:20,023 Just to make our code a bit more easy to scan. 378 00:18:20,870 --> 00:18:22,380 I hope that all of this made sense, 379 00:18:22,380 --> 00:18:24,820 this turned out to be quite a long video, 380 00:18:24,820 --> 00:18:26,780 but it was important to finish 381 00:18:26,780 --> 00:18:29,356 this part here by explaining you 382 00:18:29,356 --> 00:18:31,830 this logic here, so that in the next videos, 383 00:18:31,830 --> 00:18:34,080 we will then be able to implement 384 00:18:34,080 --> 00:18:36,110 the rest of the features. 385 00:18:36,110 --> 00:18:37,940 Because, again, without having it 386 00:18:37,940 --> 00:18:39,983 like this, it would not work. 28806

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.