1
00:00:00,300 --> 00:00:00,600
All right.

2
00:00:00,600 --> 00:00:04,920
And now let's connect to our MongoDB Atlas account.

3
00:00:05,160 --> 00:00:13,830
And before we continue, let me just mention that in between the videos, I switched back to Port 5000.

4
00:00:13,860 --> 00:00:15,690
So remember when we're testing?

5
00:00:15,750 --> 00:00:20,490
I set it up as Port 4000, so I switched back to port 5000.

6
00:00:20,790 --> 00:00:25,110
And also I remove the error from the home run.

7
00:00:25,140 --> 00:00:30,780
So if you don't want to get some weird bugs when you visit the home root, I also suggest removing that

8
00:00:30,780 --> 00:00:31,530
as well.

9
00:00:31,620 --> 00:00:33,990
And before we continue, let me just stress something.

10
00:00:34,200 --> 00:00:37,230
In my opinion, this is somewhat of an advanced course.

11
00:00:37,440 --> 00:00:47,020
So my expectation is already that you do have existing MongoDB Atlas account with proper database access

12
00:00:47,040 --> 00:00:53,690
setup with proper network access setup and you know how to already get the connection string.

13
00:00:53,700 --> 00:00:55,260
Now we'll get it together.

14
00:00:55,260 --> 00:01:00,240
But the idea is that I'm not going to set up the account from scratch.

15
00:01:00,510 --> 00:01:03,240
If you're not familiar with those things, please.

16
00:01:03,390 --> 00:01:11,670
Like I keep mentioning, go back to my Node Xpress project video since I cover all of those steps in

17
00:01:11,670 --> 00:01:16,950
great detail there and I don't want to waste everyone's time and do them one more time.

18
00:01:17,460 --> 00:01:24,120
And in order to get running we'll need a mongo installed because we'll use the connect method.

19
00:01:24,450 --> 00:01:29,930
And in order to do that, we need to grab the command here and then stop the server.

20
00:01:29,940 --> 00:01:30,970
You already know the drill.

21
00:01:30,990 --> 00:01:32,460
So let's stop the server.

22
00:01:32,670 --> 00:01:37,500
Let's clear everything and then we're going to go with NPM install Mongo.

23
00:01:37,950 --> 00:01:42,930
And as I said, I'm actually going to switch this one because I like actually how this one looks better.

24
00:01:42,930 --> 00:01:48,090
So this one is useful if you want to get the commands, but actually prefer this setup as well.

25
00:01:48,120 --> 00:01:48,940
Don't ask me why.

26
00:01:48,960 --> 00:01:50,640
Just a weird preference.

27
00:01:51,000 --> 00:01:53,970
And then we want to create a DB folder.

28
00:01:54,150 --> 00:01:59,640
Now I guess let's start up the application first and then that is going to be in a route again.

29
00:01:59,640 --> 00:02:03,060
So make sure that it's right next to the server we're looking for.

30
00:02:03,060 --> 00:02:05,190
DB And then inside of it.

31
00:02:06,060 --> 00:02:12,300
Let's create our connect JS function meaning file, but the function is going to be over there.

32
00:02:12,630 --> 00:02:15,860
And then inside of it, we want to start by getting the mongoose.

33
00:02:15,870 --> 00:02:17,220
So let's go with mongoose.

34
00:02:17,220 --> 00:02:19,200
So import mongoose.

35
00:02:19,200 --> 00:02:27,750
And that is coming from the mongoose package Mongoose here and it looks like I forgot to add from and

36
00:02:27,750 --> 00:02:31,050
let's set up the function that is going to be looking for the URL.

37
00:02:31,440 --> 00:02:34,260
So that is going to be our connection string.

38
00:02:34,260 --> 00:02:41,550
So let's go here const and then connect ADB and I'll probably mention this 20,000 times, but Mongoose

39
00:02:41,550 --> 00:02:46,680
Connect method returns a promise and therefore in server will set up async await.

40
00:02:46,680 --> 00:02:49,560
So this function will be looking for that connection string.

41
00:02:49,560 --> 00:02:51,810
And then from this function, what do we want to do?

42
00:02:51,810 --> 00:02:54,900
We want to go with a return and mongoose.

43
00:02:54,900 --> 00:02:56,340
So that's our import.

44
00:02:56,340 --> 00:02:58,680
And then we want to go with connect method.

45
00:02:58,830 --> 00:03:07,890
And if you're using Mongoose six, then you don't need to add anymore the deprecation warnings.

46
00:03:07,890 --> 00:03:13,800
So my assumption is that you use Mongoose before and prior to version six you needed to add a bunch

47
00:03:13,800 --> 00:03:14,970
of deprecation warnings.

48
00:03:14,970 --> 00:03:16,770
In this case, you don't have to do that anymore.

49
00:03:16,770 --> 00:03:19,260
You can simply just pass in the URL.

50
00:03:19,290 --> 00:03:22,110
Again, that is going to be our connection string.

51
00:03:22,110 --> 00:03:28,410
And then we want to go with export default and then let's look for connect and DB.

52
00:03:28,650 --> 00:03:29,880
Let's save this.

53
00:03:29,880 --> 00:03:33,570
And now back in the server, here's what I want to do.

54
00:03:33,810 --> 00:03:41,670
I only want to spin up my server If the connection was successful and the way we can do that, we can

55
00:03:41,670 --> 00:03:43,080
set up another function.

56
00:03:43,170 --> 00:03:48,300
So that's going to be our start function, which we invoke all the way somewhere down here.

57
00:03:48,510 --> 00:03:50,790
And let's just start by setting everything up.

58
00:03:50,790 --> 00:03:56,190
And yes, it will have to be a sync because like I just said, connect.

59
00:03:56,190 --> 00:04:00,420
DB Basically Mongoose Connect returns a promise.

60
00:04:00,420 --> 00:04:02,520
So let's go here with can't start.

61
00:04:03,520 --> 00:04:05,830
Now that is equal to my sink.

62
00:04:06,190 --> 00:04:09,580
And then inside of it, we're going to go with tri catch.

63
00:04:10,030 --> 00:04:11,260
That's an awesome start.

64
00:04:11,260 --> 00:04:13,490
And then let's go with a weight.

65
00:04:13,510 --> 00:04:19,300
Again, this is returning a promise we want to go with Connect DB And let's take a look.

66
00:04:19,899 --> 00:04:24,000
And as you can see over here, we're right away have the JS added.

67
00:04:24,010 --> 00:04:29,560
Now, this technically is not a middleware, so I don't want to change this around a little bit where

68
00:04:29,740 --> 00:04:36,130
I want to move it above the middleware and I don't think I'm going to give it a comment yet, maybe

69
00:04:36,130 --> 00:04:36,760
a little bit later.

70
00:04:36,760 --> 00:04:42,940
But I do want to make sure that there's definitely is above the middleware, just like my setup that

71
00:04:42,940 --> 00:04:43,300
way.

72
00:04:43,480 --> 00:04:46,030
And then what is this function looking for?

73
00:04:46,030 --> 00:04:47,530
It's looking for the URL, correct.

74
00:04:47,530 --> 00:04:53,290
Now we don't have the connection string yet, so we will set this one up and I know for sure that the

75
00:04:53,290 --> 00:04:57,310
name will be Mongo URL, at least in my case.

76
00:04:57,310 --> 00:05:01,750
So I'm going to go with the process since I know that I can access my environment variables that way

77
00:05:01,750 --> 00:05:08,140
and then mongo and then underscore URL again, the variable is not there yet.

78
00:05:08,350 --> 00:05:16,210
Then what we want to do is grab this app, that lesson like so and then where we have the catch.

79
00:05:16,920 --> 00:05:19,260
Well, let's just log the error for now.

80
00:05:19,680 --> 00:05:23,070
Let's say here log and then the error.

81
00:05:24,060 --> 00:05:32,490
And essentially the server will only spin up if we're successful, if we can successfully connect to

82
00:05:32,490 --> 00:05:33,330
MongoDB.

83
00:05:33,360 --> 00:05:39,330
Here I want to remove the lesson part and then right after I have the function, I want to invoke it

84
00:05:39,330 --> 00:05:40,200
as well.

85
00:05:40,230 --> 00:05:44,150
Now, of course, the moment there should be big fat error.

86
00:05:44,160 --> 00:05:44,670
Why?

87
00:05:44,700 --> 00:05:47,190
Well, because we don't have the connection string.

88
00:05:47,370 --> 00:05:48,630
So let me try it out.

89
00:05:48,630 --> 00:05:49,730
Let me save it.

90
00:05:49,740 --> 00:05:50,160
And.

91
00:05:50,160 --> 00:05:50,590
Yep.

92
00:05:50,610 --> 00:05:53,340
Notice here we have Mongo connect.

93
00:05:53,340 --> 00:05:54,420
Must be a string.

94
00:05:54,420 --> 00:05:56,720
And actually we got back on the fine.

95
00:05:56,730 --> 00:05:58,320
So what is our next step?

96
00:05:58,350 --> 00:05:59,550
Well, let's take a look at that.

97
00:05:59,550 --> 00:06:00,220
Read me.

98
00:06:00,240 --> 00:06:05,490
So we have the start function now we want to get the connection string, then we want to set up mongo

99
00:06:05,520 --> 00:06:12,720
URL and then we want to provide the credentials and the database name now where we can connect to the

100
00:06:12,720 --> 00:06:16,620
application, where we need to go to our MongoDB Atlas account.

101
00:06:16,620 --> 00:06:18,600
We're looking for connect option.

102
00:06:18,600 --> 00:06:22,320
We're looking for specifically connect your application.

103
00:06:22,320 --> 00:06:25,920
I'm going to go with Node.js 4.0 or later.

104
00:06:26,190 --> 00:06:28,800
And then we want to grab this sucker over here.

105
00:06:28,800 --> 00:06:30,210
So notice that is my user.

106
00:06:30,210 --> 00:06:36,690
John Of course the password is a dummy one at the moment and we have our first database, if that's

107
00:06:36,690 --> 00:06:37,410
what you see.

108
00:06:37,440 --> 00:06:38,240
Beautiful.

109
00:06:38,430 --> 00:06:39,510
Let's move back.

110
00:06:39,540 --> 00:06:40,440
A quick update.

111
00:06:40,440 --> 00:06:45,960
Since I recorded this video, Atlas has changed a few things about the setup.

112
00:06:46,790 --> 00:06:54,290
And even though changes are not big, it can lead to some annoying and hard to troubleshoot bugs.

113
00:06:54,710 --> 00:06:55,820
So let me cover them.

114
00:06:55,820 --> 00:06:58,820
So we're all on the same page.

115
00:06:59,270 --> 00:07:06,110
First, when it comes to database access, we need to add built in role manually.

116
00:07:06,200 --> 00:07:08,630
So let's say I want to add a new user.

117
00:07:10,490 --> 00:07:13,130
And in my case, I'm going to go with Peter.

118
00:07:14,150 --> 00:07:16,190
I'll go with some password here.

119
00:07:16,790 --> 00:07:18,530
That's the one that they generate.

120
00:07:19,010 --> 00:07:24,010
Notice how in here we need to click on add built in row.

121
00:07:24,470 --> 00:07:31,340
If you're not going to select this and if you'll just add the user, you won't be able to connect to

122
00:07:31,360 --> 00:07:33,650
database from your local application.

123
00:07:34,610 --> 00:07:37,040
Even with the correct password and username.

124
00:07:37,610 --> 00:07:44,720
So in here we manually need to click on add built in row and then select roll whether that is an admin

125
00:07:44,720 --> 00:07:45,770
only read.

126
00:07:46,280 --> 00:07:53,660
But in my case, I'm going to go with, read and write to any database, so make sure that it is selected

127
00:07:53,750 --> 00:07:54,260
here.

128
00:07:54,590 --> 00:07:59,570
Now of course if you created the user already before then it's not a big deal.

129
00:08:00,110 --> 00:08:01,940
Then there's not much you need to change.

130
00:08:01,940 --> 00:08:08,810
But if you didn't, if you're creating a new user from scratch, just make sure that you add that built

131
00:08:08,810 --> 00:08:09,890
in URL.

132
00:08:10,310 --> 00:08:14,030
Now the second one is the connect.

133
00:08:15,000 --> 00:08:21,540
So let me go back to the database and when we click on Connect and more specifically, connect your

134
00:08:21,540 --> 00:08:22,410
application.

135
00:08:22,830 --> 00:08:27,690
Now, they don't provide that my first database like I have in the video.

136
00:08:28,050 --> 00:08:31,920
So at the moment we get empty value.

137
00:08:32,070 --> 00:08:36,539
So we just need to manually add that database name, whatever that one is.

138
00:08:36,659 --> 00:08:38,880
That's also something to keep in mind.

139
00:08:39,000 --> 00:08:42,270
In the video, I have my first database in here.

140
00:08:42,270 --> 00:08:46,830
You need to have that value between the forward slash and the question mark.

141
00:08:47,370 --> 00:08:56,190
And lastly, this is really not a change, but I strongly, strongly, strongly suggest using the 000,

142
00:08:56,220 --> 00:09:02,190
meaning that you can access the database from anywhere, not the local IP.

143
00:09:03,030 --> 00:09:07,320
You can technically at your current address while you're developing.

144
00:09:07,410 --> 00:09:16,320
But once we push this up to production, if you use your local IP, it can lead to some unnecessary

145
00:09:16,320 --> 00:09:18,270
and hard to track bugs.

146
00:09:18,570 --> 00:09:26,190
And then in here in that env right after my port, I'm going to go with mongo underscore the url, lets

147
00:09:26,190 --> 00:09:28,290
pass in the connection string here.

148
00:09:28,680 --> 00:09:31,260
And then what I'm looking for here is the password.

149
00:09:31,260 --> 00:09:37,640
So the password that I have, which if I'm being perfectly honest, I think I already forgot and then

150
00:09:37,650 --> 00:09:38,790
the database name.

151
00:09:38,790 --> 00:09:45,780
Now in my case, since I have quite a few applications from the Node Express course, let me take a

152
00:09:45,780 --> 00:09:46,980
look at my collections.

153
00:09:46,980 --> 00:09:48,690
I have the last one here.

154
00:09:48,690 --> 00:09:52,680
Auth So I think I'm going to start with 20 and then hyphen job.

155
00:09:52,680 --> 00:09:56,490
If I in your case, of course you can set up whatever name you want.

156
00:09:56,490 --> 00:10:04,710
So where you have the first database over here, I'm going to go with 20 and then job if I like.

157
00:10:04,710 --> 00:10:10,080
So but of course the name is really up to you, then I'm going to change actually the password.

158
00:10:10,080 --> 00:10:16,470
I'll set it up to my correct password and then once everything's in place again, we want to restart

159
00:10:16,470 --> 00:10:21,000
the server because we're making changes here in the data entry.

160
00:10:21,000 --> 00:10:28,020
So let me add the password and switch back to server address and let me try it one more time to spin

161
00:10:28,020 --> 00:10:29,310
up our server.

162
00:10:29,400 --> 00:10:30,120
Beautiful.

163
00:10:30,150 --> 00:10:35,340
I finally remembered my password, so now I'm back in the server.

164
00:10:35,340 --> 00:10:43,380
Just I will stop the server and let's run and PM start and let's see if everything is correct.

165
00:10:43,380 --> 00:10:49,650
We should see server is listening on port 5000 and now we can move on to the next step.

