1
00:00:02,350 --> 00:00:03,769
Now, in this course section,

2
00:00:03,769 --> 00:00:05,290
as I mentioned before,

3
00:00:05,290 --> 00:00:08,180
we're going to build a basic blog website,

4
00:00:08,180 --> 00:00:09,920
which will look like this,

5
00:00:09,920 --> 00:00:13,670
and which will allow us to browse blog posts,

6
00:00:13,670 --> 00:00:16,110
to add new blog posts,

7
00:00:16,110 --> 00:00:20,220
where we also have a dropdown with predefined authors

8
00:00:20,220 --> 00:00:22,830
that can be assigned to blog posts.

9
00:00:22,830 --> 00:00:25,140
and we're then, of course, also going to be able

10
00:00:25,140 --> 00:00:27,980
to view individual blog posts.

11
00:00:27,980 --> 00:00:30,740
And we'll, of course, also be able to add

12
00:00:30,740 --> 00:00:32,369
or delete blog posts.

13
00:00:32,369 --> 00:00:34,320
So we've got all these CRUD operations

14
00:00:34,320 --> 00:00:36,433
incorporated into this website.

15
00:00:37,270 --> 00:00:38,670
So that's what we're going to build.

16
00:00:38,670 --> 00:00:42,550
And this slide here is actually from the MySQL

17
00:00:42,550 --> 00:00:45,190
and NodeJS course section.

18
00:00:45,190 --> 00:00:48,320
We're going to build the same thing with NoSQL

19
00:00:48,320 --> 00:00:49,830
and MongoDB here,

20
00:00:49,830 --> 00:00:51,880
but there will be an important difference

21
00:00:51,880 --> 00:00:55,391
when it comes to the steps we're going to go through.

22
00:00:55,391 --> 00:00:58,490
With MySQL, we planned and designed

23
00:00:58,490 --> 00:01:00,630
our databases and tables.

24
00:01:00,630 --> 00:01:03,960
And we then created those database tables.

25
00:01:03,960 --> 00:01:06,230
And then we added some initial data.

26
00:01:06,230 --> 00:01:09,060
With NoSQL and MongoDB, of course,

27
00:01:09,060 --> 00:01:13,560
the step where we create databases and tables

28
00:01:13,560 --> 00:01:15,500
will be skipped.

29
00:01:15,500 --> 00:01:19,090
And we're also going to plan our database layout

30
00:01:19,090 --> 00:01:20,780
a little bit differently.

31
00:01:20,780 --> 00:01:22,330
We're mostly going to think

32
00:01:22,330 --> 00:01:24,210
about the queries we will be running.

33
00:01:24,210 --> 00:01:26,920
And then decide which collections we want,

34
00:01:26,920 --> 00:01:29,230
and how the documents should look like.

35
00:01:29,230 --> 00:01:31,860
And then we're still going to add some initial data,

36
00:01:31,860 --> 00:01:35,050
but not through some graphical user interface,

37
00:01:35,050 --> 00:01:37,910
but instead with help of the MongoDB shell,

38
00:01:37,910 --> 00:01:40,873
which we also used before, in the course already.

39
00:01:41,760 --> 00:01:43,670
Now, last but not least, then of course,

40
00:01:43,670 --> 00:01:46,420
as the most important part of this course section,

41
00:01:46,420 --> 00:01:49,390
you are then going to see how you can also connect

42
00:01:49,390 --> 00:01:51,620
and talk to that database from inside

43
00:01:51,620 --> 00:01:54,403
the NodeJS/Express backend code.

44
00:01:55,490 --> 00:01:57,680
Now, therefore let's dive right in

45
00:01:57,680 --> 00:02:00,870
and plan our database structure.

46
00:02:00,870 --> 00:02:04,230
And as you learned, when it comes to planning

47
00:02:04,230 --> 00:02:07,830
your database structure, and which collections you want,

48
00:02:07,830 --> 00:02:10,479
and how your documents should look like,

49
00:02:10,479 --> 00:02:14,460
in a NoSQL database, then you actually wanna start

50
00:02:14,460 --> 00:02:18,230
by thinking about the queries you'll be running.

51
00:02:18,230 --> 00:02:21,550
Are you going to do a lot of writing to the database?

52
00:02:21,550 --> 00:02:24,780
A lot of inserting or a lot of reading?

53
00:02:24,780 --> 00:02:27,980
For a lot of websites, it's more reading than writing,

54
00:02:27,980 --> 00:02:32,010
but of course that's not necessarily true for every website.

55
00:02:32,010 --> 00:02:34,360
And then once you made that decision,

56
00:02:34,360 --> 00:02:36,300
it, of course, is important to understand

57
00:02:36,300 --> 00:02:39,990
which kind of data are you going to write or read.

58
00:02:39,990 --> 00:02:42,610
And you then want to create collections

59
00:02:42,610 --> 00:02:46,380
that actually minimize the effort of running

60
00:02:46,380 --> 00:02:48,653
those write or read queries.

61
00:02:49,570 --> 00:02:52,380
Now, in this website, which we're going to build here,

62
00:02:52,380 --> 00:02:53,940
we are building a blog.

63
00:02:53,940 --> 00:02:57,840
And whilst of course, we do have capabilities for adding

64
00:02:57,840 --> 00:03:01,280
and deleting and updating blog posts.

65
00:03:01,280 --> 00:03:03,520
There probably won't be

66
00:03:03,520 --> 00:03:06,840
multiple blog posts added every minute.

67
00:03:06,840 --> 00:03:09,310
So there won't be a lot of writing,

68
00:03:09,310 --> 00:03:12,960
only some occasional write operations.

69
00:03:12,960 --> 00:03:15,350
On the apprehend, it is, of course, fair to assume

70
00:03:15,350 --> 00:03:18,633
that for a blog, there will be a lot of data fetching.

71
00:03:19,470 --> 00:03:21,900
We will hopefully have a lot of visitors

72
00:03:21,900 --> 00:03:26,350
who view this starting page with all the blog posts

73
00:03:26,350 --> 00:03:29,500
or who then also viewed the individual blog posts.

74
00:03:29,500 --> 00:03:32,210
So we will often fetch all the blog posts

75
00:03:32,210 --> 00:03:36,580
or single blog posts, which we identify by ID.

76
00:03:36,580 --> 00:03:39,380
And therefore, we want to plan our collections for that.

77
00:03:40,490 --> 00:03:42,780
Now, of course, it makes sense that therefore

78
00:03:42,780 --> 00:03:46,290
create a collection that holds all our posts.

79
00:03:46,290 --> 00:03:48,380
That we create a posts collection,

80
00:03:48,380 --> 00:03:51,390
which then stores our post documents.

81
00:03:51,390 --> 00:03:54,810
So every blog post is a standalone document

82
00:03:54,810 --> 00:03:57,350
in that post's collection.

83
00:03:57,350 --> 00:04:01,410
And then every document in this post collection

84
00:04:01,410 --> 00:04:04,600
should have some unique identifier, this unique ID,

85
00:04:04,600 --> 00:04:07,690
which thankfully will be created automatically

86
00:04:07,690 --> 00:04:09,970
by MongoDB for us.

87
00:04:09,970 --> 00:04:13,250
And then of course, it's up to you how a blog post

88
00:04:13,250 --> 00:04:14,990
should look for you.

89
00:04:14,990 --> 00:04:17,070
For me, it will have a title,

90
00:04:17,070 --> 00:04:19,180
so the main title of the blog post.

91
00:04:19,180 --> 00:04:20,990
Then a short summary text,

92
00:04:20,990 --> 00:04:25,970
which I show on the page where we list all the blog posts.

93
00:04:25,970 --> 00:04:28,990
Then the detailed full text body

94
00:04:28,990 --> 00:04:31,080
that belongs to every blog post,

95
00:04:31,080 --> 00:04:34,040
which you can see if you visit the detailed page

96
00:04:34,040 --> 00:04:35,623
of a single blog post.

97
00:04:36,700 --> 00:04:39,910
And of course, I also want to have a date snapshot,

98
00:04:39,910 --> 00:04:43,630
which basically tells me when this blog post was created.

99
00:04:43,630 --> 00:04:46,730
And that will also be part of the data

100
00:04:46,730 --> 00:04:48,303
we show to the user in the end.

101
00:04:49,400 --> 00:04:52,840
And then there's a never key element

102
00:04:52,840 --> 00:04:55,280
and never a key piece of data

103
00:04:55,280 --> 00:04:57,410
that's attached to every blog post.

104
00:04:57,410 --> 00:05:00,180
And that should be information about the author

105
00:05:00,180 --> 00:05:01,433
of the blog post.

106
00:05:02,380 --> 00:05:04,670
Now, here's something important though,

107
00:05:04,670 --> 00:05:06,380
when it comes to this author,

108
00:05:06,380 --> 00:05:09,120
it's not just attached to the blog post,

109
00:05:09,120 --> 00:05:11,110
but we also need to be able to get

110
00:05:11,110 --> 00:05:15,610
a list of available authors when we add a new blog post,

111
00:05:15,610 --> 00:05:17,940
because there, I want to have a dropdown

112
00:05:17,940 --> 00:05:22,720
where you can choose from a predefined list of authors.

113
00:05:22,720 --> 00:05:26,090
So that you can't come up with arbitrary names

114
00:05:26,090 --> 00:05:29,840
or introduce typos when typing the author name.

115
00:05:29,840 --> 00:05:34,010
But that instead, this is a fixed list of predefined values

116
00:05:34,010 --> 00:05:38,700
and those values should be coming from the database as well.

117
00:05:38,700 --> 00:05:41,540
So that, again, you can't introduce errors

118
00:05:41,540 --> 00:05:43,963
by mistyping or anything like that.

119
00:05:44,840 --> 00:05:47,250
And therefore we will actually also have

120
00:05:47,250 --> 00:05:50,210
a second collection of authors.

121
00:05:50,210 --> 00:05:53,600
And there, I wanna have a unique identifier per author.

122
00:05:53,600 --> 00:05:55,030
And then let's say the name

123
00:05:55,030 --> 00:05:58,150
and email address of each author.

124
00:05:58,150 --> 00:06:00,660
And we will actually pre-populate

125
00:06:00,660 --> 00:06:03,320
this author's collection ahead of time.

126
00:06:03,320 --> 00:06:07,160
Our website won't have a section or a site

127
00:06:07,160 --> 00:06:09,610
where you can add new authors.

128
00:06:09,610 --> 00:06:11,970
Though, of course, as an extra bonus task,

129
00:06:11,970 --> 00:06:14,770
you could try to add such a site to it.

130
00:06:14,770 --> 00:06:16,730
But instead here, we will just assume

131
00:06:16,730 --> 00:06:18,630
that some global administrator

132
00:06:18,630 --> 00:06:21,430
is administrating the list of authors.

133
00:06:21,430 --> 00:06:22,970
And therefore, that will be some data,

134
00:06:22,970 --> 00:06:25,900
which we actually add through the MongoDB shell,

135
00:06:25,900 --> 00:06:27,950
and which we then can use in the website

136
00:06:27,950 --> 00:06:30,080
for populating this dropdown,

137
00:06:30,080 --> 00:06:33,113
and for connecting blog posts to authors.

138
00:06:33,971 --> 00:06:35,390
But speaking off that,

139
00:06:35,390 --> 00:06:39,690
of course, a blog post should be affiliated with an author.

140
00:06:39,690 --> 00:06:42,750
And therefore we will store the idea of an author

141
00:06:42,750 --> 00:06:44,420
for a given blog post.

142
00:06:44,420 --> 00:06:47,210
But I'll not just do that.

143
00:06:47,210 --> 00:06:51,140
In the MySQL world, we would just have stored the ID

144
00:06:51,140 --> 00:06:53,660
and then we would have joined the data together

145
00:06:53,660 --> 00:06:55,850
when we need the combined data.

146
00:06:55,850 --> 00:06:58,373
When we also need the name of the author.

147
00:06:59,370 --> 00:07:03,780
But here, for example, when I show a list of all blog posts,

148
00:07:03,780 --> 00:07:06,610
I do wanna show that author name here.

149
00:07:06,610 --> 00:07:08,680
I don't need to email address here.

150
00:07:08,680 --> 00:07:12,230
But it would be a bit annoying if we have to always join

151
00:07:12,230 --> 00:07:13,650
the two collections here

152
00:07:13,650 --> 00:07:16,410
for displaying a list of blog posts.

153
00:07:16,410 --> 00:07:20,420
If we would have to run two different query stale for.

154
00:07:20,420 --> 00:07:24,040
Remember that our goal always is to reduce the amount

155
00:07:24,040 --> 00:07:27,500
of queries and we would not achieve this here.

156
00:07:27,500 --> 00:07:30,680
Therefore, I will actually also store the author name

157
00:07:30,680 --> 00:07:32,720
in my blog posts collection.

158
00:07:32,720 --> 00:07:36,580
So the ID, and then also the name of and affiliated

159
00:07:36,580 --> 00:07:41,210
of a related author for a given blog post document.

160
00:07:41,210 --> 00:07:43,780
Of course, the name therefore, will be duplicated.

161
00:07:43,780 --> 00:07:46,810
It's in the different blog post documents,

162
00:07:46,810 --> 00:07:49,970
and it's in the authors collection documents

163
00:07:49,970 --> 00:07:51,990
in one of the documents there.

164
00:07:51,990 --> 00:07:54,120
But that shouldn't be a huge issue.

165
00:07:54,120 --> 00:07:56,840
It would be a huge issue if the name would change

166
00:07:56,840 --> 00:07:59,300
all the time, because then we have to change it

167
00:07:59,300 --> 00:08:01,390
in both the authors collection

168
00:08:01,390 --> 00:08:05,930
and possibly in a lot of documents in the posts collection.

169
00:08:05,930 --> 00:08:08,530
But names typically don't change that often.

170
00:08:08,530 --> 00:08:12,470
So this kind of duplication won't be a problem.

171
00:08:12,470 --> 00:08:15,290
We could also duplicate the email address,

172
00:08:15,290 --> 00:08:19,040
so that we never have to run an unnecessary second query

173
00:08:19,040 --> 00:08:20,310
to get this data.

174
00:08:20,310 --> 00:08:23,600
And we might implement this for all this course section,

175
00:08:23,600 --> 00:08:26,310
but of course, email addresses could change

176
00:08:26,310 --> 00:08:28,390
more frequently than names.

177
00:08:28,390 --> 00:08:30,610
So there, it really comes down to request

178
00:08:30,610 --> 00:08:34,100
to whoever that is data that you expect to change a lot.

179
00:08:34,100 --> 00:08:36,530
In which case, duplicating it would be bad

180
00:08:36,530 --> 00:08:37,820
because when it changes,

181
00:08:37,820 --> 00:08:40,090
you will have to attach a lot of documents,

182
00:08:40,090 --> 00:08:41,500
which is bad for performance

183
00:08:41,500 --> 00:08:43,620
and is a lot of work you have to do.

184
00:08:43,620 --> 00:08:46,380
If you don't expect email addresses to change a lot,

185
00:08:46,380 --> 00:08:48,060
you could also duplicated that

186
00:08:48,060 --> 00:08:50,190
into your blog posts documents,

187
00:08:50,190 --> 00:08:54,230
so that you actually never have to run queries reaching out

188
00:08:54,230 --> 00:08:57,060
to two different collections at the same time

189
00:08:57,060 --> 00:08:59,660
to get some merged data.

190
00:08:59,660 --> 00:09:01,540
Because unlike with MySQL,

191
00:09:01,540 --> 00:09:04,470
there is no join operation that you could run

192
00:09:04,470 --> 00:09:06,210
on the database level,

193
00:09:06,210 --> 00:09:09,720
instead, for NoSQL databases like MongoDB.

194
00:09:09,720 --> 00:09:12,880
If you want to join data from multiple collections,

195
00:09:12,880 --> 00:09:14,603
you'll have to do it manually.

196
00:09:15,460 --> 00:09:18,970
So I'll come back to that when we have the problem later on,

197
00:09:18,970 --> 00:09:22,460
but that's the general structure I plan on implementing.

198
00:09:22,460 --> 00:09:24,160
And throughout this course section,

199
00:09:24,160 --> 00:09:25,840
you'll see how that works,

200
00:09:25,840 --> 00:09:28,130
which potential challenges we face,

201
00:09:28,130 --> 00:09:30,530
and how we can run queries efficiently

202
00:09:30,530 --> 00:09:33,740
with that plan layout here.

203
00:09:33,740 --> 00:09:35,080
Now, with all of that,

204
00:09:35,080 --> 00:09:37,300
it's therefore time to leave the theory

205
00:09:37,300 --> 00:09:38,940
and dive into our project.

206
00:09:38,940 --> 00:09:42,930
And get started by adding or by connecting to MongoDB.

207
00:09:42,930 --> 00:09:46,673
And then also get started with adding blog posts and so on.

