1
00:00:02,520 --> 00:00:04,500
So let's get started with that,

2
00:00:04,500 --> 00:00:08,130
with implementing this kind of structure.

3
00:00:08,130 --> 00:00:09,660
And for this, I first of all

4
00:00:09,660 --> 00:00:12,070
wanna clean up my existing data.

5
00:00:12,070 --> 00:00:15,270
So for that, I'll reach out to restaurants

6
00:00:15,270 --> 00:00:18,850
and run deleteMany and actually pass in

7
00:00:18,850 --> 00:00:20,550
an empty object here,

8
00:00:20,550 --> 00:00:23,880
which is a quick trick for deleting all the documents

9
00:00:23,880 --> 00:00:25,163
in a collection.

10
00:00:27,150 --> 00:00:29,370
You'll of course, rarely wanna do that

11
00:00:29,370 --> 00:00:31,750
because it really erases everything

12
00:00:31,750 --> 00:00:34,660
but here I do wanna do that, to clear it.

13
00:00:34,660 --> 00:00:37,280
Now, with that, I deleted the last remaining restaurant,

14
00:00:37,280 --> 00:00:38,113
which I had.

15
00:00:39,290 --> 00:00:41,290
So now we can start from scratch again

16
00:00:41,290 --> 00:00:44,750
and again, I will start by actually not setting up

17
00:00:44,750 --> 00:00:47,520
by my restaurants but my types.

18
00:00:47,520 --> 00:00:50,890
Again, having that extra types collection is optional

19
00:00:50,890 --> 00:00:54,363
but I explained why I do wanna have it in the last lectures.

20
00:00:55,500 --> 00:00:59,060
So here, I now wanna start by inserting types

21
00:00:59,060 --> 00:01:02,820
and here, I'll insert one new type at a time

22
00:01:02,820 --> 00:01:05,099
and for every type, I wanna have an ID,

23
00:01:05,099 --> 00:01:10,100
which is automatically generated and the name of the type.

24
00:01:10,235 --> 00:01:12,330
So here, since the ID is automatic generated,

25
00:01:12,330 --> 00:01:16,150
we just type name and then this could be German.

26
00:01:16,150 --> 00:01:20,700
We confirm this and then we may be also add Italian

27
00:01:20,700 --> 00:01:23,380
and then maybe also American

28
00:01:23,380 --> 00:01:26,550
and whichever other types of food,

29
00:01:26,550 --> 00:01:28,033
of cuisines you wanna add.

30
00:01:30,400 --> 00:01:33,770
Now with that, with these types inserted,

31
00:01:33,770 --> 00:01:36,120
if I run db.types.find,

32
00:01:36,120 --> 00:01:39,533
we can list all these types and that's now what we got here.

33
00:01:40,730 --> 00:01:42,820
Now we can move on to the restaurants

34
00:01:43,740 --> 00:01:47,700
and here we can use restaurants and again,

35
00:01:47,700 --> 00:01:50,400
insert one new restaurant here.

36
00:01:50,400 --> 00:01:54,433
So again, we define such a JavaScript object, so to say.

37
00:01:55,300 --> 00:01:57,770
The ID will be added automatically

38
00:01:57,770 --> 00:02:00,070
but in addition, we wanna add a name

39
00:02:00,070 --> 00:02:01,730
and then this nested address

40
00:02:01,730 --> 00:02:04,133
and then also the nested type data.

41
00:02:04,990 --> 00:02:07,073
And therefore, I'll start with the name.

42
00:02:07,920 --> 00:02:12,113
Again, let's say I have the Munich Schnitzel House,

43
00:02:13,170 --> 00:02:16,880
then the address, which is this nested object,

44
00:02:16,880 --> 00:02:19,170
So another pair of curly braces,

45
00:02:19,170 --> 00:02:21,270
and then here for that address,

46
00:02:21,270 --> 00:02:25,570
we have the street, food street, let's say.

47
00:02:25,570 --> 00:02:28,850
Then the street number, there is a line break

48
00:02:28,850 --> 00:02:30,660
but again, it's one command.

49
00:02:30,660 --> 00:02:34,910
So a street number, which I'll turn into a string here

50
00:02:34,910 --> 00:02:37,580
because we could have street numbers

51
00:02:37,580 --> 00:02:39,283
like this with annotations.

52
00:02:40,560 --> 00:02:42,500
We have the postal code

53
00:02:42,500 --> 00:02:47,500
and here I'll store this as a number actually, like this.

54
00:02:48,100 --> 00:02:50,620
We could also store it as a string

55
00:02:50,620 --> 00:02:51,850
and that might be better

56
00:02:51,850 --> 00:02:55,690
in case it starts with a leading zero or anything like that

57
00:02:55,690 --> 00:02:58,460
but to also show you how you can store numbers,

58
00:02:58,460 --> 00:03:00,200
this is how you would do it.

59
00:03:00,200 --> 00:03:03,310
You create a number as you would do it in JavaScript

60
00:03:03,310 --> 00:03:05,543
without any quotes or anything like that.

61
00:03:06,830 --> 00:03:08,500
Of course, we also have the city,

62
00:03:08,500 --> 00:03:10,393
which in my case here is Munich.

63
00:03:11,520 --> 00:03:14,470
And then I also have a country,

64
00:03:14,470 --> 00:03:16,663
which in my case here, is Germany.

65
00:03:18,750 --> 00:03:21,120
So that's now the address.

66
00:03:21,120 --> 00:03:25,380
Now, after this address object, this nested object,

67
00:03:25,380 --> 00:03:27,750
I also wanna have the type field

68
00:03:27,750 --> 00:03:30,710
and now that could just be German

69
00:03:30,710 --> 00:03:33,630
but for the reasons mentioned as before,

70
00:03:33,630 --> 00:03:37,470
I will store it actually as a nested object,

71
00:03:37,470 --> 00:03:42,470
where I point at the type ID may be stored like this,

72
00:03:42,860 --> 00:03:45,030
this key name is up to you

73
00:03:45,030 --> 00:03:48,960
and that now should be the ID of the cuisine

74
00:03:48,960 --> 00:03:50,923
I stored in my types collection.

75
00:03:52,050 --> 00:03:55,210
For this, I'll create such an object ID here.

76
00:03:55,210 --> 00:03:59,040
We could also just store it as a string if we wanted to

77
00:03:59,040 --> 00:04:00,640
but I'll create an object ID

78
00:04:00,640 --> 00:04:03,080
by executing this object ID function

79
00:04:03,080 --> 00:04:07,350
and then I'll grab the ID here with the quotes,

80
00:04:07,350 --> 00:04:09,730
from the German type and insert this

81
00:04:09,730 --> 00:04:11,853
between the object ID function.

82
00:04:14,281 --> 00:04:16,190
And then separated by number, comma,

83
00:04:16,190 --> 00:04:19,450
we can insert our name,

84
00:04:19,450 --> 00:04:21,500
so that we don't need to manually

85
00:04:21,500 --> 00:04:23,410
fetch this from the types collection

86
00:04:23,410 --> 00:04:26,100
but so that we have it right here

87
00:04:26,100 --> 00:04:28,980
when we do fetch all our restaurants later

88
00:04:28,980 --> 00:04:30,920
and that's German.

89
00:04:30,920 --> 00:04:33,760
And of course here, I'm entering it on my own again

90
00:04:33,760 --> 00:04:35,380
but as I mentioned before,

91
00:04:35,380 --> 00:04:38,810
if this would be created through some web application

92
00:04:38,810 --> 00:04:42,480
where we use some form for creating restaurants,

93
00:04:42,480 --> 00:04:45,450
then we could fetch the possible allowed values

94
00:04:45,450 --> 00:04:47,070
from the types collection

95
00:04:47,070 --> 00:04:50,890
and pre-populate a dropdown with those values

96
00:04:50,890 --> 00:04:55,230
so that this is actually not a manually entered field.

97
00:04:55,230 --> 00:04:57,020
So that's really just an exception here

98
00:04:57,020 --> 00:04:58,763
because I'm using this shell.

99
00:04:59,750 --> 00:05:02,743
And with that, the first restaurant is done.

100
00:05:03,600 --> 00:05:06,420
If I now hit Enter, this was inserted

101
00:05:06,420 --> 00:05:09,600
and now we can quickly add a second restaurant here.

102
00:05:09,600 --> 00:05:13,643
And let's say this now actually should be a burger house.

103
00:05:14,840 --> 00:05:18,030
So that's the Berlin Burger House.

104
00:05:23,810 --> 00:05:28,810
And the street here is Ham Street

105
00:05:31,610 --> 00:05:36,610
12, let's say, postal code is maybe this.

106
00:05:37,620 --> 00:05:42,230
The city of course is Berlin, country's Germany

107
00:05:42,230 --> 00:05:47,230
and now the type is American, so I'll wrap this ID here.

108
00:05:47,590 --> 00:05:52,590
And then, actually clear this ID

109
00:05:52,980 --> 00:05:54,950
from the object ID function

110
00:05:54,950 --> 00:05:57,110
and paste in that new ID

111
00:05:58,390 --> 00:06:02,423
and replace German here with American.

112
00:06:04,160 --> 00:06:06,963
And then again, hit Enter to insert this.

113
00:06:09,200 --> 00:06:11,370
And with all of that done,

114
00:06:11,370 --> 00:06:15,200
if I now quickly find my restaurants,

115
00:06:15,200 --> 00:06:17,560
here are my restaurant documents

116
00:06:17,560 --> 00:06:20,800
and you can see those nested objects in them

117
00:06:20,800 --> 00:06:23,310
that hold that nested data.

118
00:06:23,310 --> 00:06:25,210
And here, we really see the difference

119
00:06:25,210 --> 00:06:27,240
between no SQL and SQL

120
00:06:27,240 --> 00:06:31,670
because with no SQL, related data is often stored together.

121
00:06:31,670 --> 00:06:32,760
As you can see it here,

122
00:06:32,760 --> 00:06:36,863
instead of split out across multiple tables or collections.

123
00:06:39,770 --> 00:06:41,420
Now with the restaurants added,

124
00:06:41,420 --> 00:06:44,450
I also wanna add some reviews and for this,

125
00:06:44,450 --> 00:06:46,350
this is the plan I came up with.

126
00:06:46,350 --> 00:06:49,060
I wanna have a separate reviews collection

127
00:06:49,060 --> 00:06:51,410
where I have that reviews data

128
00:06:51,410 --> 00:06:55,180
but in there, I'll store a link to the related restaurant

129
00:06:55,180 --> 00:06:58,160
and also the name of the related restaurant,

130
00:06:58,160 --> 00:07:00,400
so that when we fetch the reviews,

131
00:07:00,400 --> 00:07:03,790
we don't need to separately go to the restaurants collection

132
00:07:03,790 --> 00:07:06,720
just to get the names of the related restaurants

133
00:07:06,720 --> 00:07:09,570
but that instead the names are stored together

134
00:07:09,570 --> 00:07:12,070
with the reviews, so that whenever we show

135
00:07:12,070 --> 00:07:16,090
a list of reviews, we can run one query, one command

136
00:07:16,090 --> 00:07:17,640
to get all the reviews

137
00:07:17,640 --> 00:07:19,820
and then also have the restaurant names

138
00:07:19,820 --> 00:07:21,803
already with that response.

139
00:07:22,840 --> 00:07:27,520
So therefore, we can now go to db.reviews

140
00:07:27,520 --> 00:07:29,840
and actually, first of all,

141
00:07:29,840 --> 00:07:33,380
I'll reach out to restaurants and run FindOne

142
00:07:33,380 --> 00:07:37,390
with an empty object to simply find that first restaurant,

143
00:07:37,390 --> 00:07:40,440
because I'll need its ID in a second.

144
00:07:40,440 --> 00:07:42,940
And then I'll go to the reviews collection,

145
00:07:42,940 --> 00:07:44,220
which doesn't exist yet

146
00:07:44,220 --> 00:07:47,470
but which will be created on the fly, as you learned

147
00:07:47,470 --> 00:07:51,810
to insert a new review, like this.

148
00:07:51,810 --> 00:07:53,450
Now here for the reviews,

149
00:07:53,450 --> 00:07:57,300
we wanna have the name of the person who left the review.

150
00:07:57,300 --> 00:08:00,810
The ID will be created automatically, as you learned.

151
00:08:00,810 --> 00:08:02,050
We wanna have the rating,

152
00:08:02,050 --> 00:08:05,230
which should be a number between one and five.

153
00:08:05,230 --> 00:08:07,960
And then, we also wanna have the text

154
00:08:07,960 --> 00:08:11,610
that describes the review, or that explains the review.

155
00:08:11,610 --> 00:08:13,680
Then a date when the review was left

156
00:08:13,680 --> 00:08:15,283
and then the restaurant data.

157
00:08:16,220 --> 00:08:20,000
Now, let's add their reviewer data

158
00:08:20,000 --> 00:08:25,000
and that could just be something like Max Schwarzmuller.

159
00:08:25,050 --> 00:08:26,550
Alternatively, of course,

160
00:08:26,550 --> 00:08:29,340
that could also be some nested object

161
00:08:29,340 --> 00:08:32,570
where you then have a first name, Max

162
00:08:32,570 --> 00:08:37,570
and a last name, Schwarzmuller.

163
00:08:38,360 --> 00:08:41,230
It really depends on how you wanna fetch

164
00:08:41,230 --> 00:08:42,730
and store that data.

165
00:08:42,730 --> 00:08:45,960
That depends on the application you're building.

166
00:08:45,960 --> 00:08:48,300
To show you that you could store it like this,

167
00:08:48,300 --> 00:08:49,830
I will store it like this

168
00:08:49,830 --> 00:08:52,140
but just storing it as one string,

169
00:08:52,140 --> 00:08:54,560
instead of this kind of nested object

170
00:08:54,560 --> 00:08:56,173
would also have been fine.

171
00:08:57,210 --> 00:08:58,260
And theoretically,

172
00:08:58,260 --> 00:09:00,400
you can also store it in different ways

173
00:09:00,400 --> 00:09:03,130
in your different documents in that collection,

174
00:09:03,130 --> 00:09:05,930
though even though you have that flexibility,

175
00:09:05,930 --> 00:09:09,310
you'll of course typically wanna use a similar structure,

176
00:09:09,310 --> 00:09:11,240
so that when you fetch the data,

177
00:09:11,240 --> 00:09:13,825
you can use it in one of the same way

178
00:09:13,825 --> 00:09:17,350
and you don't have to check if it's now stored as a string

179
00:09:17,350 --> 00:09:19,133
or as a nested object.

180
00:09:20,270 --> 00:09:22,190
But here, I'll go for the nested object,

181
00:09:22,190 --> 00:09:25,940
simply to, again, practice this nesting aspect

182
00:09:25,940 --> 00:09:28,040
and then separated by a comma,

183
00:09:28,040 --> 00:09:30,610
we can add the next field.

184
00:09:30,610 --> 00:09:32,250
And that could be the rating,

185
00:09:32,250 --> 00:09:34,360
which should be a number between one and five

186
00:09:34,360 --> 00:09:36,730
and let's say, here, it's a three.

187
00:09:36,730 --> 00:09:40,980
Again here, I'm entering this on my own through this shell.

188
00:09:40,980 --> 00:09:44,010
In reality, this would probably be coming

189
00:09:44,010 --> 00:09:46,910
from some application and in that application,

190
00:09:46,910 --> 00:09:49,430
you could make sure that no other values

191
00:09:49,430 --> 00:09:51,933
than one and five are inserted.

192
00:09:53,850 --> 00:09:56,490
Now we also wanna have the text.

193
00:09:56,490 --> 00:09:58,140
Again, please note the line break.

194
00:09:58,140 --> 00:10:01,450
It's text here as a key name.

195
00:10:01,450 --> 00:10:04,143
And the text then, in my case here would be a string.

196
00:10:05,010 --> 00:10:07,150
Unlike in SQL, there is no difference

197
00:10:07,150 --> 00:10:09,320
between a short or a long string,

198
00:10:09,320 --> 00:10:10,460
you can simply store

199
00:10:10,460 --> 00:10:12,913
as much text here as you want in the end.

200
00:10:13,800 --> 00:10:17,063
So here I'll add, this was okay,

201
00:10:18,710 --> 00:10:22,513
could be better, as a text.

202
00:10:23,820 --> 00:10:25,090
Because as a good German,

203
00:10:25,090 --> 00:10:28,313
I of course always have something to criticize, right?

204
00:10:29,500 --> 00:10:33,600
So, next key value pair would be the date.

205
00:10:33,600 --> 00:10:35,370
Now we haven't done this before,

206
00:10:35,370 --> 00:10:38,260
so that might've been something you struggled with

207
00:10:38,260 --> 00:10:41,270
but if you ran a search for MongoDB date,

208
00:10:41,270 --> 00:10:44,253
you'll find the official documentation article here.

209
00:10:45,520 --> 00:10:46,830
And in there, you learn

210
00:10:46,830 --> 00:10:48,290
that you would then create a date

211
00:10:48,290 --> 00:10:51,370
just as you do it in JavaScript.

212
00:10:51,370 --> 00:10:54,490
And you can, for example, pass a date in this format

213
00:10:54,490 --> 00:10:56,840
to this new date function

214
00:10:56,840 --> 00:11:00,540
to define a specific year, month and day,

215
00:11:00,540 --> 00:11:03,440
or even add hours, minutes and seconds,

216
00:11:03,440 --> 00:11:04,993
depending on what you wanna do.

217
00:11:06,220 --> 00:11:08,920
Now here, I don't care about the hours, minutes and seconds,

218
00:11:08,920 --> 00:11:11,530
I just wanna point at a specific date.

219
00:11:11,530 --> 00:11:14,260
And hence here, I'll run new date

220
00:11:14,260 --> 00:11:16,220
and to new date I'll then pass

221
00:11:16,220 --> 00:11:21,220
2021-09-10, for example.

222
00:11:22,870 --> 00:11:24,913
So that would be the date I wanna store.

223
00:11:26,310 --> 00:11:27,350
Last but not least,

224
00:11:27,350 --> 00:11:29,400
we wanna store data about the restaurant

225
00:11:29,400 --> 00:11:31,790
to which this review is linked.

226
00:11:31,790 --> 00:11:33,870
For this, I'll add a restaurant key

227
00:11:33,870 --> 00:11:38,870
and that again is a nested object where I store the ID

228
00:11:38,890 --> 00:11:41,140
of the related restaurant,

229
00:11:41,140 --> 00:11:44,160
could also be named restaurant ID, of course,

230
00:11:44,160 --> 00:11:46,533
or just ID, it's up to you.

231
00:11:47,640 --> 00:11:50,540
And I'll create such an object ID here

232
00:11:50,540 --> 00:11:53,530
and then grab this ID of this restaurant

233
00:11:53,530 --> 00:11:55,460
for which I wanna create a review here

234
00:11:56,880 --> 00:12:01,880
and paste in the quoted ID between object ID.

235
00:12:01,990 --> 00:12:04,730
And then after this object ID value,

236
00:12:04,730 --> 00:12:06,810
I'll add a number of key value pair

237
00:12:06,810 --> 00:12:09,100
and that could be the name of the restaurant,

238
00:12:09,100 --> 00:12:12,170
again, so that we don't have to separately fetch this

239
00:12:12,170 --> 00:12:14,480
if we wanna have a list of all the reviews

240
00:12:14,480 --> 00:12:16,603
with the restaurant names included.

241
00:12:17,830 --> 00:12:22,830
So I'll just paste this in here as well and then hit Enter.

242
00:12:23,140 --> 00:12:27,890
Now this was inserted and therefore, now again,

243
00:12:27,890 --> 00:12:32,593
if we run reviews.find, we see our review document here.

244
00:12:33,510 --> 00:12:35,750
Now of course you can play around with that

245
00:12:35,750 --> 00:12:37,160
and insert more data.

246
00:12:37,160 --> 00:12:40,900
And indeed, I would strongly recommend that you do so.

247
00:12:40,900 --> 00:12:43,040
All the practice querying data,

248
00:12:43,040 --> 00:12:44,950
filter for certain conditions

249
00:12:44,950 --> 00:12:48,010
and then all the practice update and deleting data,

250
00:12:48,010 --> 00:12:50,520
we'll do that together in the next lecture.

251
00:12:50,520 --> 00:12:53,500
So practice the things we did learn up to this point

252
00:12:53,500 --> 00:12:55,890
before we then wrap up this section

253
00:12:55,890 --> 00:12:59,993
to then use MongoDB with node JS in the next section.

