1
00:00:02,060 --> 00:00:03,790
So to get started,

2
00:00:03,790 --> 00:00:06,430
attached to find a project snapshot,

3
00:00:06,430 --> 00:00:08,370
which is the same project

4
00:00:08,370 --> 00:00:10,910
we used in the MySQL course sections,

5
00:00:10,910 --> 00:00:13,640
but without all the MySQL logic.

6
00:00:13,640 --> 00:00:15,530
It's just a little starting state,

7
00:00:15,530 --> 00:00:17,340
which we are going to enhance

8
00:00:17,340 --> 00:00:19,003
throughout this course section.

9
00:00:20,100 --> 00:00:22,250
So you'll find this project attached.

10
00:00:22,250 --> 00:00:24,310
You can download, and unZIP it,

11
00:00:24,310 --> 00:00:26,330
move it somewhere on your hard drive

12
00:00:26,330 --> 00:00:28,030
where you wanna work on it.

13
00:00:28,030 --> 00:00:29,800
And then as a first step,

14
00:00:29,800 --> 00:00:33,580
you should open this terminal here in Visual Studio Code.

15
00:00:33,580 --> 00:00:37,460
And run NPM install to install all the dependencies

16
00:00:37,460 --> 00:00:42,350
I listed in package.JSON, the ExpressJS package for example

17
00:00:42,350 --> 00:00:45,143
so that this project is able to work correctly.

18
00:00:47,050 --> 00:00:49,910
Once you did that, you can run NPM start

19
00:00:49,910 --> 00:00:52,230
to start this development server.

20
00:00:52,230 --> 00:00:54,720
So this Node server powered by Nodemon

21
00:00:54,720 --> 00:00:57,010
for automatic reloading as you learn.

22
00:00:57,010 --> 00:01:00,130
And once this is running, you keep this up and running.

23
00:01:00,130 --> 00:01:03,690
And you can then, of course, visit this in the browser.

24
00:01:03,690 --> 00:01:05,910
It starts on port 3000.

25
00:01:05,910 --> 00:01:08,680
So if you enter local host 3000,

26
00:01:08,680 --> 00:01:10,403
you should see a page like this.

27
00:01:11,980 --> 00:01:13,840
Now, I'm zoomed in quite a bit here.

28
00:01:13,840 --> 00:01:15,540
If you zoom out, it's smaller.

29
00:01:15,540 --> 00:01:16,700
And I will say right away

30
00:01:16,700 --> 00:01:18,600
that I got some basic styling here,

31
00:01:18,600 --> 00:01:21,940
but it's not optimized for mobile or anything like that

32
00:01:21,940 --> 00:01:24,863
because I really wanna focus on the database part here.

33
00:01:26,130 --> 00:01:27,880
So we got this All Posts page

34
00:01:27,880 --> 00:01:30,130
without any posts at this point of time.

35
00:01:30,130 --> 00:01:34,260
And we got a Create Post for creating new posts.

36
00:01:34,260 --> 00:01:37,200
But at the moment, this page also doesn't work.

37
00:01:37,200 --> 00:01:39,620
We're going to add the logic to make it work

38
00:01:39,620 --> 00:01:42,090
throughout this course section.

39
00:01:42,090 --> 00:01:45,440
And therefore, let's now see how we should proceed here.

40
00:01:45,440 --> 00:01:47,790
Now, it would be nice to view all posts.

41
00:01:47,790 --> 00:01:51,700
But for that to work, we need to be able to create posts.

42
00:01:51,700 --> 00:01:53,420
Now, for this to work on the other hand,

43
00:01:53,420 --> 00:01:55,890
we need to make sure that this dropdown here

44
00:01:55,890 --> 00:01:58,560
for selecting an author works.

45
00:01:58,560 --> 00:02:01,240
At the moment, it doesn't work because at the moment,

46
00:02:01,240 --> 00:02:02,800
we have no data source

47
00:02:02,800 --> 00:02:06,780
for fetching a list of predefined authors.

48
00:02:06,780 --> 00:02:08,120
And whilst we could, of course,

49
00:02:08,120 --> 00:02:10,630
also have a hard code this in our code here,

50
00:02:10,630 --> 00:02:13,060
I actually wanna use a database collection

51
00:02:13,060 --> 00:02:15,150
that stores the author data,

52
00:02:15,150 --> 00:02:17,820
which we then query from inside our code

53
00:02:17,820 --> 00:02:21,080
to then pre-populate this rendered template here.

54
00:02:21,080 --> 00:02:24,263
And to be precise, the dropdown in this template.

55
00:02:25,200 --> 00:02:28,160
Hence, we need a MongoDB database

56
00:02:28,160 --> 00:02:30,130
with an authors collection,

57
00:02:30,130 --> 00:02:34,340
where we as an administrator already add some authors,

58
00:02:34,340 --> 00:02:35,690
which we can then use here.

59
00:02:37,040 --> 00:02:39,223
And for all of that, of course, we have to make sure

60
00:02:39,223 --> 00:02:43,190
that we got a running MongoDB database server.

61
00:02:43,190 --> 00:02:46,730
Now, on Windows, it should be running as a service already.

62
00:02:46,730 --> 00:02:49,630
If not, make sure you start this service again

63
00:02:49,630 --> 00:02:52,610
as you learn in the previous course section.

64
00:02:52,610 --> 00:02:54,590
On macOS, if it is still running,

65
00:02:54,590 --> 00:02:56,130
you don't need to do anything.

66
00:02:56,130 --> 00:02:57,440
If it's not running yet,

67
00:02:57,440 --> 00:03:02,190
you need to re-execute this mongod command with dbpath

68
00:03:02,190 --> 00:03:06,240
and log path as you learn it in the last section.

69
00:03:06,240 --> 00:03:08,610
So if you're not sure how to start your servers,

70
00:03:08,610 --> 00:03:11,890
definitely go through these setup lectures

71
00:03:11,890 --> 00:03:14,150
in the previous section again so that you do

72
00:03:14,150 --> 00:03:18,200
have a running MongoDB server on your system.

73
00:03:18,200 --> 00:03:20,650
Once you've got this MongoDB server up and running,

74
00:03:20,650 --> 00:03:22,430
make sure it stays up and running

75
00:03:22,430 --> 00:03:24,230
as long as you're working with it.

76
00:03:24,230 --> 00:03:27,850
And then we can use the Mongo Shell to connect

77
00:03:27,850 --> 00:03:28,940
to this server.

78
00:03:28,940 --> 00:03:30,910
So that's what I'll do here.

79
00:03:30,910 --> 00:03:35,910
I now established a connection like this.

80
00:03:36,010 --> 00:03:39,540
And now, of course, we can first of all, show our DBs.

81
00:03:39,540 --> 00:03:41,530
And here, I see my rating portal

82
00:03:41,530 --> 00:03:43,900
on which I worked in the last course section,

83
00:03:43,900 --> 00:03:46,400
but nothing besides that.

84
00:03:46,400 --> 00:03:47,670
And for this course section,

85
00:03:47,670 --> 00:03:49,493
we're going to add a new database.

86
00:03:50,390 --> 00:03:52,810
Now, you learn that with MongoDB,

87
00:03:52,810 --> 00:03:56,770
adding a new database simply means that you start using it.

88
00:03:56,770 --> 00:03:58,610
And then as soon as you insert data,

89
00:03:58,610 --> 00:04:01,590
all the collections and databases you are working with

90
00:04:01,590 --> 00:04:04,713
will be created automatically if they don't exist yet.

91
00:04:05,610 --> 00:04:08,160
So here, I'll start using a special database

92
00:04:08,160 --> 00:04:10,250
with the use command.

93
00:04:10,250 --> 00:04:13,970
And I wanna use the blog database.

94
00:04:13,970 --> 00:04:14,820
The name is up to you,

95
00:04:14,820 --> 00:04:17,750
but since we are working on a blog in this course section,

96
00:04:17,750 --> 00:04:19,680
this should be a quite fitting name.

97
00:04:19,680 --> 00:04:21,173
So I'll use blog here.

98
00:04:22,800 --> 00:04:25,030
Now, with that here,

99
00:04:25,030 --> 00:04:27,970
we can start inserting some data already.

100
00:04:27,970 --> 00:04:30,160
And as I mentioned a couple of minutes ago,

101
00:04:30,160 --> 00:04:32,560
I wanna insert some author data

102
00:04:32,560 --> 00:04:35,663
so that we can use that for creating our posts.

103
00:04:36,570 --> 00:04:39,170
Hence, here, we can refer to the database

104
00:04:39,170 --> 00:04:43,340
we are currently working with, with the DB keyword.

105
00:04:43,340 --> 00:04:47,310
And then use dot to access a collection on that database.

106
00:04:47,310 --> 00:04:50,060
And here, I'll access the authors collection.

107
00:04:50,060 --> 00:04:51,780
Though this collection name, of course,

108
00:04:51,780 --> 00:04:53,830
is always up to you because as you learn,

109
00:04:53,830 --> 00:04:57,343
it will be created on the fly if it doesn't exist yet.

110
00:04:59,050 --> 00:05:01,600
And then we learn in the last course section

111
00:05:01,600 --> 00:05:03,800
that we can simply insert a new document

112
00:05:03,800 --> 00:05:06,220
with the insertOne command.

113
00:05:06,220 --> 00:05:08,230
And then passing the document

114
00:05:08,230 --> 00:05:10,723
with curly braces to insertOne.

115
00:05:12,090 --> 00:05:14,500
Now, as laid out a couple of minutes ago,

116
00:05:14,500 --> 00:05:17,960
an author should have a unique ID, a name,

117
00:05:17,960 --> 00:05:19,193
and an email address.

118
00:05:20,120 --> 00:05:22,660
Now, the ID will be generated automatically.

119
00:05:22,660 --> 00:05:24,840
So therefore, we only need to add a name

120
00:05:24,840 --> 00:05:28,050
and an email address to that author document.

121
00:05:28,050 --> 00:05:29,880
And hence, it will be a rather simple,

122
00:05:29,880 --> 00:05:32,460
straightforward document here.

123
00:05:32,460 --> 00:05:34,800
I will just add a name key here.

124
00:05:34,800 --> 00:05:39,000
And for example, name him Maximilian Schwarzmuller.

125
00:05:39,000 --> 00:05:40,730
And add an email key here.

126
00:05:40,730 --> 00:05:43,020
And that could be max@test.com,

127
00:05:43,020 --> 00:05:45,103
which of course is a fake email address.

128
00:05:47,410 --> 00:05:51,050
With that, we can hit Enter and this was inserted.

129
00:05:51,050 --> 00:05:53,580
And I'm then going to repeat this command here

130
00:05:53,580 --> 00:05:55,560
by pressing the up arrow.

131
00:05:55,560 --> 00:05:58,200
And I'm only going to swap the data

132
00:05:58,200 --> 00:06:01,553
and add a second author here, my colleague Manual Lorenz.

133
00:06:02,600 --> 00:06:07,600
And use manual@test.com as an email address.

134
00:06:08,020 --> 00:06:10,233
And also confirm this by hitting Enter.

135
00:06:12,240 --> 00:06:14,480
So now, that was inserted as well.

136
00:06:14,480 --> 00:06:17,490
And now, to confirm that this worked, we can of course,

137
00:06:17,490 --> 00:06:21,850
run db.authers.find to find all the authors

138
00:06:21,850 --> 00:06:23,610
that are stored in there.

139
00:06:23,610 --> 00:06:25,330
And there, we got the two authors

140
00:06:25,330 --> 00:06:28,163
with the automatically generated IDs.

141
00:06:29,820 --> 00:06:34,140
So with that, we pre-populated our database as needed.

142
00:06:34,140 --> 00:06:37,050
And we can now go back to our real website here,

143
00:06:37,050 --> 00:06:39,740
to our NodeJS code more importantly.

144
00:06:39,740 --> 00:06:42,190
And start connecting to MongoDB

145
00:06:42,190 --> 00:06:45,603
and working with MongoDB there as well.

