1
00:00:07,900 --> 00:00:08,800
This video.

2
00:00:08,800 --> 00:00:17,140
Let's see how we can connect our application to MongoDB server so that we can start to work with what's

3
00:00:17,170 --> 00:00:18,790
Mongo DB.

4
00:00:18,820 --> 00:00:23,110
So here we go on the MongoDB JS file.

5
00:00:23,140 --> 00:00:26,440
Let's go ahead and require the package we install.

6
00:00:26,440 --> 00:00:30,280
That is the MongoDB from the previous video.

7
00:00:30,280 --> 00:00:35,950
So now let me collapse this and now let me require the MongoDB package.

8
00:00:36,250 --> 00:00:38,500
And on that we have different methods.

9
00:00:38,500 --> 00:00:48,160
So I'm going to structure what we want so required and then the MongoDB package and here we go.

10
00:00:48,430 --> 00:00:54,340
At this point we are not going to use Mongoose, you are going to use purely MongoDB as you move on.

11
00:00:54,340 --> 00:00:57,310
We are going to use Mongoose to make our life easier.

12
00:00:57,310 --> 00:00:58,540
So let's go.

13
00:00:58,630 --> 00:01:04,540
What I want for MongoDB is what we call Mongo client.

14
00:01:04,810 --> 00:01:07,150
Mongo client.

15
00:01:07,150 --> 00:01:10,060
So this one and that is it.

16
00:01:10,480 --> 00:01:18,010
So the next step is we need to have what is called the connection string, meaning the connection string

17
00:01:18,010 --> 00:01:21,700
that we are going to use to connect to MongoDB.

18
00:01:21,730 --> 00:01:30,280
So where can we get connections training after creating account with MongoDB we have access to our connection

19
00:01:30,280 --> 00:01:33,940
string, but let's see example of the connection string.

20
00:01:33,940 --> 00:01:41,410
So this is where we lived and that the process of installing MongoDB and if we scroll down and connect

21
00:01:41,410 --> 00:01:48,010
to MongoDB, let's click on that and let's click on the Connection Guide.

22
00:01:48,010 --> 00:01:50,110
So click on that.

23
00:01:50,410 --> 00:01:53,830
And here we have what is called the connection string.

24
00:01:53,830 --> 00:01:56,170
So what is the connection string?

25
00:01:56,170 --> 00:02:05,200
Well, the connection string is a set of instructions that the driver uses to connect to a MongoDB deployment.

26
00:02:05,230 --> 00:02:13,090
It instruct the driver on how to connect to MongoDB and how it should have why connected.

27
00:02:13,090 --> 00:02:20,500
So let's look at the formats we have MongoDB, which is a protocol, and then the username password

28
00:02:20,500 --> 00:02:22,030
with our credentials.

29
00:02:22,090 --> 00:02:28,780
Remember upon creating we provided username and password and I told you that save the password somewhere

30
00:02:28,780 --> 00:02:30,640
else because you may need it.

31
00:02:30,640 --> 00:02:36,880
And what comes after add is what you call the hostname and then with the connection options.

32
00:02:36,880 --> 00:02:39,310
So how can we get our connection string?

33
00:02:39,310 --> 00:02:40,480
It's pretty easy.

34
00:02:40,480 --> 00:02:45,310
So back to the MongoDB dashboard and then let's click on database.

35
00:02:45,730 --> 00:02:51,130
And next step is we are going to connect application to our application.

36
00:02:51,130 --> 00:02:53,530
So let's click on Connect.

37
00:02:54,990 --> 00:02:56,610
And here you go.

38
00:02:56,760 --> 00:03:03,660
You can see that we have couple of options connect to MongoDB, share, like I said, and connect your

39
00:03:03,660 --> 00:03:04,550
application.

40
00:03:04,560 --> 00:03:13,080
So let's click on the second one, connect your application and here is asking us to select our driver

41
00:03:13,080 --> 00:03:15,300
because we are working with MongoDB.

42
00:03:15,330 --> 00:03:18,330
Let's select the driver and then the version.

43
00:03:18,330 --> 00:03:21,480
Let's select the latest version which is 4.1.

44
00:03:21,480 --> 00:03:28,290
So make sure that you select 4.1 and after that the next step says that add your connection string.

45
00:03:28,290 --> 00:03:30,450
So this is our connection code.

46
00:03:30,450 --> 00:03:37,830
Like we explained before, we have the protocol here, the user name and password, and the other option

47
00:03:37,830 --> 00:03:43,200
is that so click on this one and now back to our application.

48
00:03:43,200 --> 00:03:46,200
Let's create a variable called you are error.

49
00:03:46,200 --> 00:03:52,770
So cost you are error is equal to code and paste the connection string.

50
00:03:52,770 --> 00:03:56,190
So this is same as we have explored before.

51
00:03:56,580 --> 00:04:00,710
The next step is we need to provide the password.

52
00:04:00,730 --> 00:04:06,480
You remember I asked you to work to save your password, but in case you have forgotten, don't worry,

53
00:04:06,480 --> 00:04:07,620
we can get it back.

54
00:04:07,890 --> 00:04:11,310
So the next step is click on database access.

55
00:04:11,310 --> 00:04:15,120
So if you want to add a new user, you can go ahead and do that.

56
00:04:15,330 --> 00:04:16,019
All right.

57
00:04:16,230 --> 00:04:23,790
So the next step is network access of which we have done it when we're configuring our account.

58
00:04:23,880 --> 00:04:30,540
So for this IP address, anyone can have access to our MongoDB, meaning that a person can create can

59
00:04:30,540 --> 00:04:34,170
delete config based on our business logic.

60
00:04:34,440 --> 00:04:44,640
So in case you have forgotten your password, click on the database access and next is click on edit

61
00:04:44,640 --> 00:04:51,360
and here you can edit your password as that generate and copy and that is it.

62
00:04:51,540 --> 00:04:56,790
Well, next step is let's connect our application to MongoDB.

63
00:04:56,820 --> 00:05:00,120
But here is a problem one.

64
00:05:00,120 --> 00:05:06,750
Since we are going to talk to external server, we don't know when the server or the connection will

65
00:05:06,750 --> 00:05:07,770
be successful.

66
00:05:08,100 --> 00:05:13,440
It can happen that our connection will fail or it will be successful.

67
00:05:13,440 --> 00:05:16,320
So we need to handle all these cases.

68
00:05:16,410 --> 00:05:19,530
So we have couple of options to handle this.

69
00:05:19,530 --> 00:05:25,410
One, we can use callback or we can use promise or we can use async await.

70
00:05:25,680 --> 00:05:26,880
Let's get going.

71
00:05:26,880 --> 00:05:34,830
I'm going to use arrow function and the name of my connection going to be as DB connect equal to my

72
00:05:34,830 --> 00:05:37,110
arrow function as that.

73
00:05:37,290 --> 00:05:44,910
And since I'm going to use async await, I'll mark my function as an async and inside a function body,

74
00:05:44,910 --> 00:05:48,450
our brain might try and catch inside a try.

75
00:05:48,450 --> 00:05:55,830
I'm going to implement if everything goes on well and inside the catch, if something goes wrong, I'm

76
00:05:55,830 --> 00:05:57,360
going to display some message.

77
00:05:57,360 --> 00:05:59,340
So now here we go.

78
00:05:59,430 --> 00:06:05,580
The next step is I need to create an instance of my new MongoDB client.

79
00:06:05,580 --> 00:06:07,140
So here we go.

80
00:06:07,470 --> 00:06:20,610
So I'll give it a comment as create new client and make use of cost and client equal to new and make

81
00:06:20,610 --> 00:06:24,060
use of Mongo clients, which is this one.

82
00:06:24,060 --> 00:06:28,110
And for this our pass in the connection string which is this.

83
00:06:28,110 --> 00:06:32,370
So our pass in the you are error and that is it.

84
00:06:32,670 --> 00:06:38,880
So inside here, let's go ahead and connect to DB or MongoDB.

85
00:06:38,970 --> 00:06:42,930
So I'm going to await the client instance on that.

86
00:06:42,930 --> 00:06:51,360
We have a method called connect and as a function or method as that and inside the catch, if something

87
00:06:51,360 --> 00:06:57,660
goes wrong, why not go ahead and console.log the error as that.

88
00:06:57,660 --> 00:07:02,970
The next step is I need to call this function DB connect.

89
00:07:02,970 --> 00:07:10,560
So now save it and let's run the server or the application or the file and let's see if we are going

90
00:07:10,560 --> 00:07:12,930
to see connection successful.

91
00:07:12,930 --> 00:07:20,610
So here inside the try, let's provide a message to the user if the connection was successful.

92
00:07:20,610 --> 00:07:22,440
So I would say that DB.

93
00:07:23,590 --> 00:07:27,280
Connected successfully.

94
00:07:28,380 --> 00:07:29,280
All right, save it.

95
00:07:29,280 --> 00:07:30,870
And now back to the terminal.

96
00:07:30,900 --> 00:07:35,370
Make use of node and then the file is MongoDB.

97
00:07:35,400 --> 00:07:36,240
Hit enter.

98
00:07:36,720 --> 00:07:40,610
And if we see that DB connected successfully.

99
00:07:40,620 --> 00:07:47,130
So at this point we are able to connect our application to MongoDB.

100
00:07:47,370 --> 00:07:56,820
So now it's time to explore more about MongoDB, how we can create record and save it into our MongoDB

101
00:07:56,850 --> 00:07:58,350
account here.

102
00:07:58,590 --> 00:08:04,020
All right, so let's try to change the password and let's see if we're going to get an error.

103
00:08:04,020 --> 00:08:09,840
So I have remove some portion of my password, save it back to the terminal.

104
00:08:10,020 --> 00:08:12,240
Restarted and let's see.

105
00:08:13,040 --> 00:08:20,900
And you can see I have beautiful error, mongo server error, bad authentication meaning that I have

106
00:08:20,900 --> 00:08:26,200
provided that password or username, which is true.

107
00:08:26,210 --> 00:08:30,500
So let me bring it back to defaults and save it.

108
00:08:30,860 --> 00:08:33,350
And let's run the file again.

109
00:08:33,350 --> 00:08:35,380
And here you go.

110
00:08:35,390 --> 00:08:36,350
Please come.

111
00:08:36,890 --> 00:08:39,110
MongoDB connected.

112
00:08:39,140 --> 00:08:39,740
Great.

113
00:08:39,740 --> 00:08:44,690
So now it's time for us to learn more about MongoDB.

114
00:08:44,720 --> 00:08:53,000
The next video we will start with growth operations, how we can create, record, delete, update and

115
00:08:53,000 --> 00:08:53,690
fetch all.

116
00:08:53,690 --> 00:08:56,090
Let's get into that the next video.

