1
00:00:08,039 --> 00:00:13,080
In this video, let's connect our application to MongoDB.

2
00:00:13,230 --> 00:00:19,200
But before we get into that, in the previous video, I gave this one to you as a challenge that is

3
00:00:19,200 --> 00:00:25,820
refactor to the account and then the transaction route use MVC design pattern.

4
00:00:25,830 --> 00:00:32,009
If you couldn't solve it, don't worry, I have provided a source code for it and better so we will

5
00:00:32,009 --> 00:00:36,510
revisit the controls as we implement the actual logic.

6
00:00:36,600 --> 00:00:41,550
So for this video, let's go ahead and then connect to MongoDB.

7
00:00:41,730 --> 00:00:47,370
I assume that you have your MongoDB connection string ready if you don't have it.

8
00:00:47,370 --> 00:00:51,910
I advise you to watch the section and get in connection string.

9
00:00:51,930 --> 00:00:57,060
So with that being said, first step is let's go ahead and then install Mongoose.

10
00:01:11,980 --> 00:01:14,230
Now Mongoose has finished installing.

11
00:01:14,260 --> 00:01:19,670
Next is let's restart our server and let's continue the implementation.

12
00:01:19,690 --> 00:01:23,350
So here we go inside the config folder.

13
00:01:23,380 --> 00:01:27,790
Let's create one file and call this one as DB connect.

14
00:01:29,460 --> 00:01:30,690
It's genius.

15
00:01:31,440 --> 00:01:35,640
And inside this fire, let's require the mongoose package.

16
00:01:42,080 --> 00:01:46,490
The next step is let's create the function to connect to TV.

17
00:01:46,760 --> 00:01:47,750
So here we go.

18
00:01:47,750 --> 00:01:48,050
And.

19
00:02:02,080 --> 00:02:08,080
And for this it takes in the connection string and this is my connection string.

20
00:02:08,080 --> 00:02:10,930
So I would paste it here as that.

21
00:02:10,930 --> 00:02:16,570
And for this one I want to name my database by using what is called.

22
00:02:22,490 --> 00:02:23,680
A save it.

23
00:02:23,690 --> 00:02:24,110
Great.

24
00:02:24,110 --> 00:02:29,450
So inside the cat, in case something goes wrong, let's go ahead and then console.log.

25
00:02:29,450 --> 00:02:34,130
The actual error on that we have the message.

26
00:02:38,010 --> 00:02:45,240
And if you couldn't connect, let's go ahead and then exit our server that is processed dot exit and

27
00:02:45,240 --> 00:02:48,600
you pass in one for exiting the application.

28
00:02:48,780 --> 00:02:51,540
And here in case everything goes right.

29
00:02:51,570 --> 00:02:58,170
Let's go ahead and say that DB connected successfully.

30
00:03:01,230 --> 00:03:02,070
All right.

31
00:03:02,340 --> 00:03:08,760
So for this, I want to call the function automatically here without exporting it, meaning that as

32
00:03:08,760 --> 00:03:12,210
soon as I require this file, this function will be run.

33
00:03:12,360 --> 00:03:16,170
So let's go ahead and then require it inside the server.

34
00:03:16,170 --> 00:03:18,070
And this is how I'm going to require it.

35
00:03:18,090 --> 00:03:23,220
So going to be as required and then goes for this last config.

36
00:03:23,220 --> 00:03:25,820
And inside there we have the DB connect.

37
00:03:25,830 --> 00:03:29,400
Now let's check our terminal and let's have a look.

38
00:03:29,490 --> 00:03:32,390
You can see that we have some error connecting.

39
00:03:32,400 --> 00:03:34,560
So let's look at the connection string.

40
00:03:34,560 --> 00:03:41,730
Well, in case you're not doing something right, oh, I have to remove the additional code around it,

41
00:03:41,850 --> 00:03:43,430
and that is it.

42
00:03:43,440 --> 00:03:45,150
Let's check it out and see.

43
00:03:45,450 --> 00:03:49,460
Moment of truth and there we go.

44
00:03:49,470 --> 00:03:54,390
DB Connected successfully, so now we are done with that one.

45
00:03:54,660 --> 00:04:02,160
Guys, before we continue, I have changed the part from 9000 to 8080 because that part, which is 10,000,

46
00:04:02,190 --> 00:04:06,120
is being used by different application on my operating system.

47
00:04:06,120 --> 00:04:07,560
So that is it.

48
00:04:07,680 --> 00:04:13,200
So next step is that let me show you how I'm going to use the extension code MongoDB.

49
00:04:13,290 --> 00:04:21,060
If we install the extension that is MongoDB for VTS code, click on that and here click on Add connection.

50
00:04:21,060 --> 00:04:24,570
And for this, let's bring in our connection string.

51
00:04:24,570 --> 00:04:29,580
So let me copy and let's go to the extension and click on Connect.

52
00:04:29,670 --> 00:04:30,480
Click on that.

53
00:04:30,480 --> 00:04:37,170
And here it says that paste the connection string and let me go ahead and then paste that and hit enter.

54
00:04:37,560 --> 00:04:40,260
Now something is going to happen.

55
00:04:40,320 --> 00:04:44,490
Let's wait a there we go.

56
00:04:44,520 --> 00:04:46,290
You can see that connected.

57
00:04:46,500 --> 00:04:51,630
So let's click on the extension and here is my database.

58
00:04:51,630 --> 00:05:00,450
So here we can visualize what is going on inside our database right inside Visual Studio code in the

59
00:05:00,450 --> 00:05:01,230
next video.

60
00:05:01,230 --> 00:05:06,450
Let's go ahead and then model our data for this particular application.

