Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,960 --> 00:00:02,940
So the filter feature that we have
2
00:00:02,940 --> 00:00:06,250
implemented right now already works great,
3
00:00:06,250 --> 00:00:09,860
but in this video we wanna take it to the next level
4
00:00:09,860 --> 00:00:12,733
by allowing some even more complex queries.
5
00:00:14,210 --> 00:00:17,680
So right now a user can only filter the documents
6
00:00:17,680 --> 00:00:22,430
by setting one key equal to a value, right?
7
00:00:22,430 --> 00:00:24,760
But now, we actually want to also implement
8
00:00:24,760 --> 00:00:27,680
the greater than, the greater or equal than,
9
00:00:27,680 --> 00:00:31,733
the less than, and the less or equal than operators.
10
00:00:32,780 --> 00:00:34,510
So instead of just having equal,
11
00:00:34,510 --> 00:00:37,500
we want to actually be able to, for example,
12
00:00:37,500 --> 00:00:40,130
say duration greater or equal than five,
13
00:00:40,130 --> 00:00:42,290
and not just equal, all right?
14
00:00:42,290 --> 00:00:45,310
And so that makes everything a bit more complex.
15
00:00:45,310 --> 00:00:48,140
Now, before writing any code, I will actually
16
00:00:48,140 --> 00:00:50,810
do the exercise of writing out the query
17
00:00:50,810 --> 00:00:53,043
how it should look like in MongoDB.
18
00:00:55,150 --> 00:00:58,200
So what would the filter object actually look like?
19
00:00:58,200 --> 00:01:00,360
And let's write it here as a comment,
20
00:01:00,360 --> 00:01:02,273
and again, just as an exercise.
21
00:01:03,270 --> 00:01:06,090
So the object would look a bit like this,
22
00:01:06,090 --> 00:01:09,723
so difficulty, start with that one, set to easy,
23
00:01:14,040 --> 00:01:17,030
and then duration; and now remember that when
24
00:01:17,030 --> 00:01:21,000
we want to use an operator, we need to start another object.
25
00:01:21,000 --> 00:01:24,530
And then for the greater or equal than operator
26
00:01:24,530 --> 00:01:29,530
we use it like this, to five, right, remember that?
27
00:01:30,640 --> 00:01:33,050
So this is how we would manually write
28
00:01:33,050 --> 00:01:36,383
the filter object for the query that we just specified.
29
00:01:37,340 --> 00:01:40,240
Now let's actually go back to Postman
30
00:01:40,240 --> 00:01:43,860
so that I can show you a very kind of standard way
31
00:01:43,860 --> 00:01:47,440
of writing a query string including these operators.
32
00:01:47,440 --> 00:01:50,670
And we do it like this, so before the equals sign
33
00:01:50,670 --> 00:01:53,150
we open up the brackets and then in here
34
00:01:53,150 --> 00:01:55,880
we write the operator, so in this case
35
00:01:55,880 --> 00:01:58,650
greater than or equal, and that's it.
36
00:01:58,650 --> 00:02:01,880
And so basically like this, we add a third part
37
00:02:01,880 --> 00:02:04,680
to the key value pair, so we have the key,
38
00:02:04,680 --> 00:02:07,610
the value, and now also the operator.
39
00:02:07,610 --> 00:02:09,930
And we use again these brackets
40
00:02:09,930 --> 00:02:11,923
in order to specify the operator.
41
00:02:13,060 --> 00:02:15,320
So again, that is a standard way
42
00:02:15,320 --> 00:02:17,170
and so let's now actually go ahead
43
00:02:17,170 --> 00:02:21,150
and take a look at the query string that Express gives us
44
00:02:21,150 --> 00:02:23,360
and so just like before, let's actually
45
00:02:23,360 --> 00:02:25,823
log a reg dot query to the console,
46
00:02:27,310 --> 00:02:32,310
log reg dot query, all right.
47
00:02:32,600 --> 00:02:35,530
And so let's take a look what our query object
48
00:02:35,530 --> 00:02:37,640
is gonna look like when we specify
49
00:02:37,640 --> 00:02:39,940
the query string like this.
50
00:02:39,940 --> 00:02:42,040
All right, now the first thing that you notice
51
00:02:42,040 --> 00:02:43,640
is that we get an error here,
52
00:02:43,640 --> 00:02:45,573
but don't worry about that for now.
53
00:02:46,570 --> 00:02:50,470
So what I'm interested here, is this object,
54
00:02:50,470 --> 00:02:52,920
and so let me actually go ahead and copy it here.
55
00:02:55,570 --> 00:02:58,820
All right, so what do you notice here?
56
00:02:58,820 --> 00:03:01,420
Well, what we see here is that the query object
57
00:03:01,420 --> 00:03:03,870
looks almost identical to the filter object
58
00:03:03,870 --> 00:03:06,350
that we wrote manually, the only difference
59
00:03:06,350 --> 00:03:10,513
is that in this one we have the MongoDB operator sign here.
60
00:03:11,360 --> 00:03:13,420
So that's really the only thing that's missing here
61
00:03:13,420 --> 00:03:16,210
in front of this operator name.
62
00:03:16,210 --> 00:03:18,620
Okay, and so the solution for this
63
00:03:18,620 --> 00:03:21,780
is to basically replace all the operators like this
64
00:03:21,780 --> 00:03:24,300
with their correspondent MongoDB operators,
65
00:03:24,300 --> 00:03:29,030
so basically adding this dollar sign here, okay.
66
00:03:29,030 --> 00:03:31,270
So let's now implement that,
67
00:03:31,270 --> 00:03:33,433
and in fact we do it somewhere up here,
68
00:03:34,810 --> 00:03:36,960
let's actually add some more comments here.
69
00:03:38,700 --> 00:03:42,090
So filtering that we did in the last lecture,
70
00:03:42,090 --> 00:03:45,313
now here let's add some advanced filtering,
71
00:03:47,920 --> 00:03:50,903
and so that's the code that we're gonna write now.
72
00:03:52,720 --> 00:03:56,393
Now let's put this code here right at the top, all right.
73
00:03:57,290 --> 00:03:59,860
Then this code here we don't really need
74
00:03:59,860 --> 00:04:01,160
but I still want to keep it here
75
00:04:01,160 --> 00:04:03,193
so let's put it just here at the end.
76
00:04:04,960 --> 00:04:08,270
And then just this code here as a reference
77
00:04:08,270 --> 00:04:11,210
just to make sure that we know what we're doing here.
78
00:04:11,210 --> 00:04:13,330
All right, and so now that we know
79
00:04:13,330 --> 00:04:15,230
what we need to do here, it is actually
80
00:04:15,230 --> 00:04:17,399
just a simple Javascript problem.
81
00:04:17,399 --> 00:04:19,420
And the way I'm solving it is this,
82
00:04:19,420 --> 00:04:22,710
so first I'm gonna convert the object to a string
83
00:04:25,230 --> 00:04:30,230
so query string equals JSON dot stringify of query object,
84
00:04:35,600 --> 00:04:38,780
and so now I can use that query string
85
00:04:38,780 --> 00:04:40,963
to use the replace function on it.
86
00:04:42,060 --> 00:04:44,530
All right, and so what I want to replace now
87
00:04:44,530 --> 00:04:48,330
is the gte with dollar sign gte,
88
00:04:48,330 --> 00:04:50,403
and then also some other operator.
89
00:04:51,360 --> 00:04:53,290
So the ones we want to replace are
90
00:04:53,290 --> 00:04:56,113
greater than or equal, greater than,
91
00:04:56,970 --> 00:05:01,010
less than or equal, and less than,
92
00:05:01,010 --> 00:05:03,620
and there are a couple ways in which we can do it,
93
00:05:03,620 --> 00:05:06,193
but I'm gonna use a regular expression.
94
00:05:07,243 --> 00:05:10,340
And for me personally, regular expressions
95
00:05:10,340 --> 00:05:13,450
are one of the most difficult concepts in Javascript
96
00:05:13,450 --> 00:05:16,210
and actually in general in programming.
97
00:05:16,210 --> 00:05:18,270
So usually when I need to do something like this,
98
00:05:18,270 --> 00:05:21,538
I simply go on Stack Overflow or just Google around
99
00:05:21,538 --> 00:05:24,180
to find the right solution for the problem
100
00:05:24,180 --> 00:05:26,280
that I'm trying to solve, all right.
101
00:05:26,280 --> 00:05:28,070
And so what we want to do now here
102
00:05:28,070 --> 00:05:31,660
is to basically match one of these four words
103
00:05:31,660 --> 00:05:33,900
and then replace it with the same words
104
00:05:33,900 --> 00:05:35,933
but with the dollar sign in front.
105
00:05:36,980 --> 00:05:38,530
And so the regular expression to
106
00:05:38,530 --> 00:05:42,620
find one of these four words goes like this;
107
00:05:42,620 --> 00:05:46,820
so we open up these parenthesis and then we write
108
00:05:46,820 --> 00:05:50,517
the operators, so gte or gt or lte or lt.
109
00:05:55,500 --> 00:06:00,500
And then we also need to add this \b before and after,
110
00:06:01,140 --> 00:06:04,283
and that's because we only want to match these exact words.
111
00:06:05,220 --> 00:06:08,183
So imagine there's a word which has lt in it,
112
00:06:08,183 --> 00:06:10,700
then we of course do not want to match that
113
00:06:10,700 --> 00:06:14,070
we only want to match if it is this exact word,
114
00:06:14,070 --> 00:06:16,840
without any other string around it.
115
00:06:16,840 --> 00:06:19,170
And so this regular expression that I have here
116
00:06:19,170 --> 00:06:21,310
will take care of that, and actually there is
117
00:06:21,310 --> 00:06:24,670
one piece missing and that is this g flag here
118
00:06:24,670 --> 00:06:27,130
which means that it will actually happen multiple times.
119
00:06:27,130 --> 00:06:30,270
So if we have like two or three operators
120
00:06:30,270 --> 00:06:33,880
or even all of them, then it will replace all of them.
121
00:06:33,880 --> 00:06:36,240
And without this g here, it would only
122
00:06:36,240 --> 00:06:40,300
replace the first occurrence, so this one is very important.
123
00:06:40,300 --> 00:06:43,460
Now, what do we actually want to replace it with?
124
00:06:43,460 --> 00:06:45,450
Well, the replace method actually
125
00:06:45,450 --> 00:06:48,880
accepts a callback which is very powerful,
126
00:06:48,880 --> 00:06:51,100
and this callback has as the first argument,
127
00:06:51,100 --> 00:06:53,523
the matched word, or the matched string.
128
00:06:55,060 --> 00:06:58,690
So we can pass match into that callback function
129
00:06:58,690 --> 00:07:01,840
and what we return from this callback is the new string
130
00:07:01,840 --> 00:07:04,690
that will replace the old one, okay?
131
00:07:04,690 --> 00:07:07,200
So let's do a template string here,
132
00:07:07,200 --> 00:07:08,820
and so we want to replace the match
133
00:07:08,820 --> 00:07:13,820
with dollar sign match, okay, so this is here
134
00:07:14,190 --> 00:07:17,060
the part of the template string syntax.
135
00:07:17,060 --> 00:07:19,860
So this here is this match, and then
136
00:07:19,860 --> 00:07:22,613
we want to put the dollar sign in front of that.
137
00:07:25,690 --> 00:07:28,570
And now just to make sure, let's actually log it
138
00:07:29,970 --> 00:07:34,250
to the console and actually want the object back,
139
00:07:34,250 --> 00:07:38,303
so JSON dot parse the query string.
140
00:07:40,210 --> 00:07:43,200
All right, let me just go ahead and clear this here
141
00:07:43,200 --> 00:07:45,603
so that you can see the results a bit better.
142
00:07:48,541 --> 00:07:50,050
So again, we get this error here
143
00:07:50,050 --> 00:07:52,240
and that's of course because MongoDB
144
00:07:52,240 --> 00:07:54,330
cannot really use the query string or
145
00:07:54,330 --> 00:07:56,933
the query object as we have it right now.
146
00:07:58,980 --> 00:08:01,300
And here we have the two results
147
00:08:01,300 --> 00:08:04,610
and you actually see that our result
148
00:08:04,610 --> 00:08:07,150
is the exact same as before and so
149
00:08:07,150 --> 00:08:09,670
something here is clearly not working.
150
00:08:09,670 --> 00:08:12,240
So let's try to figure out what that is.
151
00:08:12,240 --> 00:08:15,940
And here the regular expression looks just fine,
152
00:08:15,940 --> 00:08:18,707
everything looks fine here, ah, but okay,
153
00:08:18,707 --> 00:08:22,470
the problem is that I'm replacing it in the string
154
00:08:22,470 --> 00:08:24,320
but then I'm not saving it anywhere.
155
00:08:24,320 --> 00:08:28,030
And so actually we want to save it back to the query string.
156
00:08:28,030 --> 00:08:30,580
So let's change this here to a let,
157
00:08:30,580 --> 00:08:33,940
so that it can then mutate the data
158
00:08:33,940 --> 00:08:38,940
and so let's say, query string, oh sorry.
159
00:08:39,640 --> 00:08:44,640
So, query string equals the query string replaced,
160
00:08:45,530 --> 00:08:49,750
just like before, so what I was forgetting is simply to
161
00:08:49,750 --> 00:08:52,693
actually save the result here back into the variable.
162
00:08:54,320 --> 00:08:58,670
Let's try that again, and now it's working.
163
00:08:58,670 --> 00:09:01,170
So now we have the dollar sign
164
00:09:01,170 --> 00:09:04,400
in front of the operator, great.
165
00:09:04,400 --> 00:09:07,840
Okay, so it works, let's get rid of these comments here
166
00:09:07,840 --> 00:09:10,430
and now finally, all we have to do is to actually here
167
00:09:10,430 --> 00:09:13,703
not use the query object but just use this.
168
00:09:16,730 --> 00:09:19,290
So JSON.parse query string, and of course
169
00:09:19,290 --> 00:09:22,020
if you do not have any operator in the query string
170
00:09:22,020 --> 00:09:23,770
then everything will work just fine.
171
00:09:23,770 --> 00:09:27,920
It will simply not find any of these four operators there
172
00:09:27,920 --> 00:09:29,670
but that's no problem, it will then
173
00:09:30,539 --> 00:09:31,403
simply not replace anything.
174
00:09:32,681 --> 00:09:36,290
And also let's go ahead and try this out
175
00:09:37,690 --> 00:09:39,800
and it's taking a bit of time but indeed
176
00:09:39,800 --> 00:09:41,750
it came back with three results,
177
00:09:41,750 --> 00:09:45,020
and so let's now check if they're actually correct.
178
00:09:45,020 --> 00:09:47,860
So the duration should be greater or equal than five,
179
00:09:47,860 --> 00:09:50,570
and this one here is duration of five,
180
00:09:50,570 --> 00:09:52,730
this has a duration of nine, which is
181
00:09:52,730 --> 00:09:54,880
greater than or equal than five,
182
00:09:54,880 --> 00:09:58,730
and so it's actually working, the difficulty is still easy
183
00:09:58,730 --> 00:10:02,460
and so what it means is that the code
184
00:10:02,460 --> 00:10:04,603
that we just implemented works just fine.
185
00:10:05,480 --> 00:10:08,750
Perfect, now let's just for the fun of it
186
00:10:08,750 --> 00:10:11,380
add another parameter up there,
187
00:10:11,380 --> 00:10:13,860
so let's filter for the price now as well.
188
00:10:13,860 --> 00:10:18,860
So this one has almost 2000, then 1100, this one has 300.
189
00:10:19,350 --> 00:10:23,070
So let's now filter for prices less than a thousand,
190
00:10:23,070 --> 00:10:25,150
or less than a thousand five hundred.
191
00:10:28,100 --> 00:10:32,920
And price, and now again our brackets,
192
00:10:32,920 --> 00:10:37,920
and we use less than 1500, and all right, send that.
193
00:10:41,320 --> 00:10:46,240
And what do we get here, oh, now we only have two results.
194
00:10:46,240 --> 00:10:51,240
And indeed, the prices here, 397, and here 1197,
195
00:10:51,420 --> 00:10:54,930
and so the one before that was 2000 is now gone.
196
00:10:54,930 --> 00:10:57,470
So we filtered that one out as well
197
00:10:57,470 --> 00:11:00,670
using again the operator that we just implemented.
198
00:11:00,670 --> 00:11:03,030
And we could now filter for all kinds of stuff here
199
00:11:03,030 --> 00:11:07,060
but you get the point, and so yeah,
200
00:11:07,060 --> 00:11:10,110
we implemented our kind of advanced filtering here
201
00:11:10,110 --> 00:11:14,500
at this point, which now even accepts these four operators.
202
00:11:14,500 --> 00:11:16,600
Now in the real world, we would then have to
203
00:11:16,600 --> 00:11:19,340
write some documentation, basically in order to allow
204
00:11:19,340 --> 00:11:22,040
the user to know which kinds of operation
205
00:11:22,040 --> 00:11:25,100
they can do on our API, right?
206
00:11:25,100 --> 00:11:28,800
So again, ideally we would completely document our API,
207
00:11:28,800 --> 00:11:31,660
specifying which requests can be made
208
00:11:31,660 --> 00:11:34,360
using which htp methods, and then also
209
00:11:34,360 --> 00:11:36,490
what kind of filtering or sorting,
210
00:11:36,490 --> 00:11:39,580
or all these features, which of them are available
211
00:11:39,580 --> 00:11:41,570
and how they can use it, right?
212
00:11:41,570 --> 00:11:44,400
Now, in our case we of course will not do that
213
00:11:44,400 --> 00:11:47,000
but again, don't forget that if you're really implementing
214
00:11:47,000 --> 00:11:50,160
an API that is gonna be used by someone else,
215
00:11:50,160 --> 00:11:53,600
then you must really do this kind of documentation.
216
00:11:53,600 --> 00:11:56,780
But anyway, let's now move on to our next video
217
00:11:56,780 --> 00:11:59,203
and implement a sorting functionality.
17830
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.