﻿1
00:00:01,090 --> 00:00:04,160
‫Querying for data in a database is one

2
00:00:04,160 --> 00:00:08,320
‫of the most important operations that we have in databases.

3
00:00:08,320 --> 00:00:11,820
‫And so, let's now take a look at a couple of query operators

4
00:00:11,820 --> 00:00:14,770
‫in MongoDB, starting with some simple ones

5
00:00:14,770 --> 00:00:17,993
‫and then moving on to some really complex queries.

6
00:00:19,610 --> 00:00:23,010
‫And, to start, let's give ourselves some space here.

7
00:00:23,010 --> 00:00:25,760
‫And so, you already know, the easiest way

8
00:00:25,760 --> 00:00:28,480
‫to basically query for all the documents

9
00:00:28,480 --> 00:00:31,470
‫in a certain collection is to just use find

10
00:00:31,470 --> 00:00:33,560
‫without passing anything in there.

11
00:00:33,560 --> 00:00:36,130
‫So, that's what we've been doing until this point.

12
00:00:36,130 --> 00:00:38,570
‫And so, it gives us simply this result

13
00:00:38,570 --> 00:00:40,040
‫with all the documents

14
00:00:40,040 --> 00:00:42,110
‫that are in a certain collection basically

15
00:00:42,110 --> 00:00:44,620
‫without any searching criteria.

16
00:00:44,620 --> 00:00:47,910
‫But, now, let's say that we actually only want one tour

17
00:00:47,910 --> 00:00:49,540
‫and we already know its name.

18
00:00:49,540 --> 00:00:51,610
‫And so, we can search for that tour

19
00:00:51,610 --> 00:00:54,120
‫using the name that we know.

20
00:00:54,120 --> 00:00:57,910
‫And so, we us db.tours

21
00:00:59,150 --> 00:01:00,400
‫and, again, find.

22
00:01:00,400 --> 00:01:03,573
‫But, this time, we're gonna pass in a filter object.

23
00:01:04,930 --> 00:01:06,710
‫So, again, we need an object here.

24
00:01:06,710 --> 00:01:09,580
‫And so, you start to see now that in MongoDB,

25
00:01:09,580 --> 00:01:12,560
‫really everything works with objects.

26
00:01:12,560 --> 00:01:15,330
‫And, this will be even more prominent a bit later

27
00:01:15,330 --> 00:01:16,163
‫in this lecture.

28
00:01:17,265 --> 00:01:20,790
‫So, inside this object, we pass in the filter.

29
00:01:20,790 --> 00:01:22,650
‫So, basically the search criteria

30
00:01:22,650 --> 00:01:25,050
‫that we want to search for.

31
00:01:25,050 --> 00:01:28,343
‫So, we simply set the name to the tour name

32
00:01:28,343 --> 00:01:30,570
‫that we want to search for.

33
00:01:30,570 --> 00:01:32,120
‫So, let's say The Forest Hiker.

34
00:01:35,040 --> 00:01:36,330
‫And, that's actually it.

35
00:01:36,330 --> 00:01:39,750
‫So, that is our search criteria or the search filter.

36
00:01:39,750 --> 00:01:41,260
‫And, if we had returned now,

37
00:01:41,260 --> 00:01:43,580
‫then we only get this one tour

38
00:01:43,580 --> 00:01:47,553
‫where the name matches exactly the one that we passed in.

39
00:01:48,970 --> 00:01:51,830
‫And, we could do, of course, the same for anything else.

40
00:01:51,830 --> 00:01:53,760
‫For example, for the difficulty,

41
00:01:53,760 --> 00:01:57,800
‫we could search for tours

42
00:01:57,800 --> 00:01:59,593
‫that have an easy in difficulty.

43
00:02:01,890 --> 00:02:06,343
‫So, difficulty and easy.

44
00:02:07,640 --> 00:02:10,030
‫Close the object, close the function.

45
00:02:10,030 --> 00:02:13,910
‫And so, we get all the tours where the difficulty is easy.

46
00:02:13,910 --> 00:02:16,420
‫Right now, there is only one tour,

47
00:02:16,420 --> 00:02:19,010
‫but if we had multiple tours with difficulty set

48
00:02:19,010 --> 00:02:20,260
‫to easy, then, of course,

49
00:02:20,260 --> 00:02:23,020
‫it would return all of these documents.

50
00:02:23,020 --> 00:02:25,150
‫So, that is the easiest way

51
00:02:25,150 --> 00:02:27,400
‫that we can search for documents.

52
00:02:27,400 --> 00:02:29,610
‫Now, let's take it to the next level

53
00:02:29,610 --> 00:02:32,063
‫by using some special query operators.

54
00:02:33,100 --> 00:02:36,070
‫And, what I want to do is to search for tours

55
00:02:36,070 --> 00:02:38,673
‫which have a price below 500.

56
00:02:40,470 --> 00:02:43,670
‫And, the way it works is like this.

57
00:02:43,670 --> 00:02:48,670
‫So, we always use db. The collection name .find.

58
00:02:49,170 --> 00:02:51,670
‫And then, again, or filter object.

59
00:02:51,670 --> 00:02:54,620
‫And so, remember, I want to search for prices,

60
00:02:54,620 --> 00:02:56,430
‫so I say price.

61
00:02:56,430 --> 00:03:00,550
‫And now, I want all the tours with a price below 500.

62
00:03:00,550 --> 00:03:02,090
‫So, how do I do that?

63
00:03:02,090 --> 00:03:05,000
‫Well, I need to use the less than operator

64
00:03:05,000 --> 00:03:06,630
‫and it works like this.

65
00:03:06,630 --> 00:03:09,080
‫We need to define yet a new object

66
00:03:09,080 --> 00:03:13,017
‫where we set the lte property to 500,

67
00:03:15,090 --> 00:03:16,550
‫then close that object,

68
00:03:16,550 --> 00:03:19,320
‫and then close that first price object

69
00:03:19,320 --> 00:03:21,410
‫and then close the function.

70
00:03:21,410 --> 00:03:23,950
‫So, that looks very weird, I know,

71
00:03:23,950 --> 00:03:27,130
‫but this is how we use query operators in MongoDB.

72
00:03:27,130 --> 00:03:30,200
‫So, again, lte stands for less than

73
00:03:30,200 --> 00:03:32,070
‫because that is what we're searching for.

74
00:03:32,070 --> 00:03:36,140
‫Where the price is less than 500.

75
00:03:36,140 --> 00:03:38,820
‫And, this special sign here is reserved in MongoDB

76
00:03:38,820 --> 00:03:40,490
‫for its operators.

77
00:03:40,490 --> 00:03:43,520
‫So, whenever you see this dollar sign here in MongoDB,

78
00:03:43,520 --> 00:03:45,933
‫you know that it's a Mongo operator.

79
00:03:46,900 --> 00:03:48,690
‫So, the weirdest part here is probably

80
00:03:48,690 --> 00:03:51,430
‫that we have to do it inside a new object,

81
00:03:51,430 --> 00:03:54,600
‫but if you think about it, it's actually really the only way

82
00:03:54,600 --> 00:03:58,430
‫to specify that the price should not simply be 500,

83
00:03:58,430 --> 00:03:59,530
‫but something else.

84
00:03:59,530 --> 00:04:03,320
‫So, we have to set the price to something else than 500.

85
00:04:03,320 --> 00:04:04,760
‫And, the best way really is

86
00:04:04,760 --> 00:04:06,870
‫to just use another object in there.

87
00:04:06,870 --> 00:04:09,453
‫So, that's exactly how MongoDB then works.

88
00:04:10,479 --> 00:04:13,280
‫So, let's try this out now, hit return.

89
00:04:13,280 --> 00:04:15,600
‫And, indeed, we get our two documents

90
00:04:15,600 --> 00:04:20,500
‫where the price property is below 500.

91
00:04:20,500 --> 00:04:23,530
‫Great, next up, let's actually search

92
00:04:23,530 --> 00:04:26,840
‫for two search criteria at the same time.

93
00:04:26,840 --> 00:04:29,690
‫So, what I want to do next is to search for documents

94
00:04:29,690 --> 00:04:32,910
‫which have the price less or equal than 500,

95
00:04:32,910 --> 00:04:34,760
‫which is what we had here already,

96
00:04:34,760 --> 00:04:37,910
‫but also, at the same time, the rating greater

97
00:04:37,910 --> 00:04:39,903
‫or equal to 4.8.

98
00:04:41,800 --> 00:04:46,040
‫And so, that should then give us only this tour here.

99
00:04:46,040 --> 00:04:47,240
‫So, only this one

100
00:04:47,240 --> 00:04:50,590
‫because this is the only one which has the price below 500

101
00:04:50,590 --> 00:04:55,140
‫and the rating equal or greater to 4.8.

102
00:04:55,140 --> 00:04:57,780
‫So, it's 4.8 and so that one is the one

103
00:04:57,780 --> 00:05:00,150
‫that should match our query.

104
00:05:00,150 --> 00:05:03,620
‫So, let's clear it to take away some of the confusion.

105
00:05:03,620 --> 00:05:05,670
‫Now, I want this result here

106
00:05:05,670 --> 00:05:08,550
‫so that we can compare it with the next result.

107
00:05:08,550 --> 00:05:11,833
‫And so, let's now write out the query that I just described.

108
00:05:13,710 --> 00:05:17,260
‫So, find and, again, this one here is

109
00:05:17,260 --> 00:05:18,590
‫gonna be just the same.

110
00:05:18,590 --> 00:05:22,690
‫So, we want the price to be less or equal than 500.

111
00:05:22,690 --> 00:05:27,093
‫Or, we could actually simply say less than 500.

112
00:05:28,630 --> 00:05:33,630
‫So, lte is less than or equal and lt is simply less than.

113
00:05:35,690 --> 00:05:39,030
‫So, this is the first part, the price less than 500.

114
00:05:39,030 --> 00:05:41,420
‫And, at the same time, we want the rating

115
00:05:41,420 --> 00:05:43,003
‫to be greater or equal to 4.8.

116
00:05:43,970 --> 00:05:44,980
‫And, that's easy.

117
00:05:44,980 --> 00:05:47,900
‫All we have to do is to specify a second field

118
00:05:47,900 --> 00:05:49,350
‫in our filter object.

119
00:05:49,350 --> 00:05:51,823
‫So, first, the price and now the rating.

120
00:05:53,330 --> 00:05:55,150
‫So, we want the rating to be

121
00:05:56,340 --> 00:05:57,840
‫and you can probably guess it,

122
00:05:58,880 --> 00:06:03,203
‫greater than or equal 4.8.

123
00:06:05,080 --> 00:06:09,420
‫Now, close this filter object and close the function.

124
00:06:09,420 --> 00:06:11,040
‫So, let's test it out.

125
00:06:11,040 --> 00:06:14,360
‫And, indeed, it gives us the result that we were expecting.

126
00:06:14,360 --> 00:06:18,520
‫So, the only document where both the search criteria here

127
00:06:18,520 --> 00:06:20,840
‫are true at the same time.

128
00:06:20,840 --> 00:06:23,310
‫So, just to recap, when we want to search

129
00:06:23,310 --> 00:06:25,670
‫for two criteria at the same time,

130
00:06:25,670 --> 00:06:28,540
‫which basically is an and query,

131
00:06:28,540 --> 00:06:31,950
‫so price less than 500, and rating greater

132
00:06:31,950 --> 00:06:32,953
‫or equal to 4.8.

133
00:06:34,070 --> 00:06:36,000
‫Well, the only thing that we have to do is

134
00:06:36,000 --> 00:06:39,080
‫to specify two fields in the filter object.

135
00:06:39,080 --> 00:06:41,600
‫And, I know this looks quite confusing.

136
00:06:41,600 --> 00:06:45,760
‫I felt exactly the same when I was learning this stuff,

137
00:06:45,760 --> 00:06:47,800
‫but I hope that you can still follow me

138
00:06:48,870 --> 00:06:53,790
‫so that we can now actually take it even one level further.

139
00:06:53,790 --> 00:06:56,940
‫So, here, we did an and query.

140
00:06:56,940 --> 00:07:00,100
‫So, querying for documents where these two conditions

141
00:07:00,100 --> 00:07:03,820
‫are both true, but now let's do an or query.

142
00:07:03,820 --> 00:07:06,470
‫So, basically searching for all the documents

143
00:07:06,470 --> 00:07:09,070
‫where either this part here is true

144
00:07:09,070 --> 00:07:11,103
‫or this part is true.

145
00:07:13,350 --> 00:07:18,090
‫So, db.tours.find

146
00:07:18,090 --> 00:07:20,050
‫and let's close it here already

147
00:07:20,050 --> 00:07:22,653
‫because this is gonna be an even more confusing one.

148
00:07:26,000 --> 00:07:29,410
‫So, we want to do an or query and the way it works

149
00:07:29,410 --> 00:07:32,530
‫with MongoDB operators is like this.

150
00:07:32,530 --> 00:07:37,530
‫We say or and then here, we specify an array.

151
00:07:38,949 --> 00:07:42,520
‫And, in this array, we will then put the two conditions

152
00:07:42,520 --> 00:07:45,003
‫where we want one of them to be true.

153
00:07:46,714 --> 00:07:48,710
‫So, again, I know this looks confusing,

154
00:07:48,710 --> 00:07:52,310
‫but let's write it out now and I will then explain it again

155
00:07:52,310 --> 00:07:53,863
‫when we're already doing that.

156
00:07:55,580 --> 00:07:59,220
‫So, the first condition is the price should

157
00:07:59,220 --> 00:08:02,653
‫be less than 500.

158
00:08:03,730 --> 00:08:05,823
‫So, basically the same as before.

159
00:08:06,670 --> 00:08:08,623
‫So, that is the first object.

160
00:08:09,600 --> 00:08:12,130
‫So, all of this, so the first condition,

161
00:08:12,130 --> 00:08:14,040
‫basically the first filter,

162
00:08:14,040 --> 00:08:15,240
‫and then the second one.

163
00:08:16,990 --> 00:08:20,750
‫So, the rating should be, just like before,

164
00:08:20,750 --> 00:08:25,750
‫greater or equal than 4.8.

165
00:08:26,250 --> 00:08:28,513
‫Close this one and close this one.

166
00:08:29,630 --> 00:08:31,800
‫And so, that's actually already it.

167
00:08:31,800 --> 00:08:36,550
‫So, again, to recap here, we start with the or operator

168
00:08:36,550 --> 00:08:40,030
‫and the or operator accepts an array of conditions.

169
00:08:40,030 --> 00:08:43,150
‫So, that's why we then create this array here

170
00:08:43,150 --> 00:08:45,610
‫and this array will then contain one object

171
00:08:45,610 --> 00:08:48,470
‫for each of our filters basically.

172
00:08:48,470 --> 00:08:51,650
‫So, we want either this one to be true

173
00:08:51,650 --> 00:08:53,143
‫or this one.

174
00:08:55,920 --> 00:09:00,130
‫So, let's try to see what our results are gonna be.

175
00:09:00,130 --> 00:09:04,120
‫And so, actually we get all of the three tours.

176
00:09:04,120 --> 00:09:06,357
‫And so, let's analyze why that is.

177
00:09:06,357 --> 00:09:09,913
‫So, the first one has a price less than 500.

178
00:09:11,260 --> 00:09:13,470
‫And so, that's why it got included here.

179
00:09:13,470 --> 00:09:16,820
‫It doesn't have the rating greater or equal to 4.8,

180
00:09:16,820 --> 00:09:20,940
‫so it's just 4.7, but since we're doing an or query here,

181
00:09:20,940 --> 00:09:23,730
‫only one of the conditions needs to be true.

182
00:09:23,730 --> 00:09:26,250
‫And so, that's why this document here got selected

183
00:09:26,250 --> 00:09:28,180
‫and is included in our output.

184
00:09:28,180 --> 00:09:32,940
‫Next up, this one also has the price less or equal than 500

185
00:09:32,940 --> 00:09:35,800
‫and also, at the same time, it has the rating great

186
00:09:35,800 --> 00:09:37,810
‫or equal than 4.8.

187
00:09:37,810 --> 00:09:39,940
‫And so, it got included actually

188
00:09:39,940 --> 00:09:41,623
‫for both of them being true.

189
00:09:43,530 --> 00:09:47,980
‫Then, the last one does not have the price less than 500,

190
00:09:47,980 --> 00:09:52,123
‫but it does have the rating greater or equal to 4.8.

191
00:09:53,210 --> 00:09:56,210
‫And so, again, one of the two conditions is true

192
00:09:56,210 --> 00:09:58,830
‫and since we're doing an or query,

193
00:09:58,830 --> 00:10:00,483
‫this tour also got included.

194
00:10:02,020 --> 00:10:04,080
‫So, let's clear this and only one more,

195
00:10:04,080 --> 00:10:06,170
‫which is gonna be kind of the same.

196
00:10:06,170 --> 00:10:11,080
‫I'm simply gonna change from less than to greater than.

197
00:10:11,080 --> 00:10:12,630
‫And, what I want you to do now is

198
00:10:12,630 --> 00:10:14,910
‫to guess the output of this one based

199
00:10:14,910 --> 00:10:16,420
‫on the previous result.

200
00:10:16,420 --> 00:10:18,820
‫So, can you guess what the result will be?

201
00:10:18,820 --> 00:10:20,770
‫So, let's take a look.

202
00:10:20,770 --> 00:10:24,490
‫And so, now, indeed, we no longer have all three tours,

203
00:10:24,490 --> 00:10:26,750
‫but only the second and the third one

204
00:10:26,750 --> 00:10:28,510
‫because, remember, the first tour

205
00:10:28,510 --> 00:10:30,760
‫had the price less than 500

206
00:10:30,760 --> 00:10:32,460
‫and also the rating less than 4.8.

207
00:10:33,350 --> 00:10:35,820
‫And so, none of the conditions were applying

208
00:10:35,820 --> 00:10:37,700
‫and so it's no longer included.

209
00:10:37,700 --> 00:10:39,490
‫Only the second and the third one,

210
00:10:39,490 --> 00:10:43,480
‫which now has the price also not greater than 500,

211
00:10:43,480 --> 00:10:45,600
‫but it has the rating of 4.8

212
00:10:45,600 --> 00:10:47,510
‫and so it's still included.

213
00:10:47,510 --> 00:10:50,090
‫And, the last one, of course, has everything true

214
00:10:50,090 --> 00:10:52,310
‫and so, of course, it's also here.

215
00:10:52,310 --> 00:10:54,980
‫Just one more thing that I wanted to show you here is

216
00:10:54,980 --> 00:10:58,290
‫that besides our filter object, so this one,

217
00:10:58,290 --> 00:11:01,600
‫we can also pass in an object for projection.

218
00:11:01,600 --> 00:11:04,610
‫So, what projection means is that we simply want

219
00:11:04,610 --> 00:11:07,650
‫to select some of the fields in the output.

220
00:11:07,650 --> 00:11:10,310
‫So, let me show it to you and it's very simple.

221
00:11:10,310 --> 00:11:15,310
‫All we have to do is, for example, say name equals to one.

222
00:11:15,510 --> 00:11:18,570
‫So, what this means is that we only want the name

223
00:11:18,570 --> 00:11:22,670
‫to be in the output and so that's why we set name to one.

224
00:11:22,670 --> 00:11:25,520
‫All the others are not gonna appear in this case.

225
00:11:25,520 --> 00:11:26,780
‫So, let me show that to you.

226
00:11:26,780 --> 00:11:28,770
‫And, indeed, we only have the name

227
00:11:28,770 --> 00:11:30,870
‫and no longer all these other properties.

228
00:11:30,870 --> 00:11:33,913
‫And, this can be very useful in some cases.

229
00:11:35,130 --> 00:11:37,610
‫The idea is, of course, also showing up,

230
00:11:37,610 --> 00:11:38,880
‫but that's always there.

231
00:11:38,880 --> 00:11:41,300
‫There's no way of removing that.

232
00:11:41,300 --> 00:11:42,780
‫Okay, cool.

233
00:11:42,780 --> 00:11:46,460
‫So, I think the main goal of this lecture is now achieved,

234
00:11:46,460 --> 00:11:48,450
‫which was basically to get you familiar

235
00:11:48,450 --> 00:11:51,200
‫with the way we query in MongoDB

236
00:11:51,200 --> 00:11:55,570
‫and also show you these complex MongoDB operators

237
00:11:55,570 --> 00:11:57,090
‫that we have here.

238
00:11:57,090 --> 00:11:58,590
‫For some reason, I cannot select it,

239
00:11:58,590 --> 00:11:59,970
‫but you know what I mean.

240
00:11:59,970 --> 00:12:01,540
‫So, we have greater than,

241
00:12:01,540 --> 00:12:04,050
‫we have or, and we really have a ton

242
00:12:04,050 --> 00:12:05,540
‫of other operators still.

243
00:12:05,540 --> 00:12:07,360
‫So, this is only the surface,

244
00:12:07,360 --> 00:12:09,960
‫but throughout the rest of the course, you will, of course,

245
00:12:09,960 --> 00:12:12,990
‫get to know a couple of other operators.

246
00:12:12,990 --> 00:12:15,260
‫So, this is just to get you familiar,

247
00:12:15,260 --> 00:12:18,250
‫get you started with querying in MongoDB.

248
00:12:18,250 --> 00:12:21,093
‫And, I think in this lecture, we did that successfully.

