1
00:00:07,500 --> 00:00:08,970
Welcome back.

2
00:00:09,000 --> 00:00:12,330
Let's get started with Mongoose.

3
00:00:12,690 --> 00:00:19,830
So to get started with Mongoose first, unless we connect our application to Mongoose.

4
00:00:19,860 --> 00:00:27,570
So in this video, we are going to learn how we can connect our application or express application to

5
00:00:27,600 --> 00:00:28,420
Mongoose.

6
00:00:28,440 --> 00:00:30,120
So let's start it.

7
00:00:30,390 --> 00:00:38,250
The first step is we need to create a folder for this particular project by making use of m.K, d,

8
00:00:38,280 --> 00:00:43,800
i r and the name of the folder our Carmine RS Mongoose Guide.

9
00:00:44,790 --> 00:00:48,930
Hit Enter next is let me see the into the mongoose guide.

10
00:00:49,170 --> 00:00:57,420
Let me open within my Visual Studio code and I have it open here as that.

11
00:00:57,660 --> 00:01:02,030
The next step is how can we install Mongoose?

12
00:01:02,040 --> 00:01:13,500
So if you go to Mongoose Gamescom, this is the official website for Mongoose and this is the official

13
00:01:13,530 --> 00:01:16,650
website for Mongoose.

14
00:01:17,160 --> 00:01:26,940
Lucky for us, Mongoose is being hoisted on NTP and meaning we can install Mongoose for NPM if we search

15
00:01:26,970 --> 00:01:29,160
for Mongoose and PM.

16
00:01:29,160 --> 00:01:32,130
You're going to see this particular page.

17
00:01:32,160 --> 00:01:34,170
Let's read this description.

18
00:01:34,170 --> 00:01:43,200
It says that Mongoose is a MongoDB object modeling to design to work in asynchronous environment.

19
00:01:43,200 --> 00:01:50,790
And lucky for us, Mongoose support both promises and callbacks, meaning that we can make use of async,

20
00:01:50,790 --> 00:01:53,790
await promises or callbacks.

21
00:01:53,790 --> 00:01:55,440
So let's see how.

22
00:01:55,530 --> 00:02:04,680
And down here you can see how popular this package is, about more than 1 million downloads per week.

23
00:02:04,680 --> 00:02:07,610
So let's go ahead and install Mongoose.

24
00:02:07,620 --> 00:02:14,070
Well, for us to be on the same pipeline, make sure you install the exact version of Mongoose.

25
00:02:14,070 --> 00:02:16,800
That is 6.5.2.

26
00:02:16,800 --> 00:02:20,070
So let's go ahead and then install it.

27
00:02:20,070 --> 00:02:27,960
So we are going to work with Mongoose alone without using Xpress and after that we will combine express

28
00:02:27,960 --> 00:02:30,420
to interact with Mongoose also.

29
00:02:30,420 --> 00:02:37,890
So back to the terminal or better stuff, you can use the built in terminal to issue the command.

30
00:02:37,890 --> 00:02:50,310
So let me come here and go ahead and install Mongoose and p m Install Mongoose version 6.5.3.

31
00:02:50,310 --> 00:02:52,770
Let me confirm again the exact version.

32
00:02:52,770 --> 00:02:53,610
Exactly.

33
00:02:53,730 --> 00:02:57,750
So let me go ahead and then install Mongoose.

34
00:02:59,480 --> 00:03:03,110
Now Mongoose has finished installing.

35
00:03:03,140 --> 00:03:06,410
So how can we run the application?

36
00:03:06,470 --> 00:03:12,470
Let's go ahead and create one file core server or mongoose.

37
00:03:12,500 --> 00:03:16,070
Let's make it as Mongoose dart js.

38
00:03:16,770 --> 00:03:20,340
Ain't your going to write everything on one file?

39
00:03:20,430 --> 00:03:27,240
If you go to the official website and on the home page, you will see how we can connect to Mongoose

40
00:03:27,240 --> 00:03:31,710
with two lines of code as compared to MongoDB.

41
00:03:31,740 --> 00:03:38,820
We wrote at least many lines to get it done, so let's see how we are going to do this one inside our

42
00:03:38,820 --> 00:03:39,720
application.

43
00:03:40,080 --> 00:03:50,660
So first step is we need to require the package core mongoose equal to require the mongoose package.

44
00:03:50,670 --> 00:03:53,010
And here we go.

45
00:03:53,400 --> 00:03:57,450
The next step is let's go ahead and connect to Mongoose.

46
00:03:57,450 --> 00:03:58,410
So here we go.

47
00:03:58,410 --> 00:03:59,310
Connect.

48
00:04:00,460 --> 00:04:03,550
To Mongoose.

49
00:04:03,700 --> 00:04:04,690
All right.

50
00:04:05,620 --> 00:04:08,180
I forgot to bring one over here.

51
00:04:08,200 --> 00:04:09,340
So here we go.

52
00:04:09,580 --> 00:04:13,210
On Mongoose, we have a method called Connect.

53
00:04:13,450 --> 00:04:15,460
And that is it, guys.

54
00:04:15,460 --> 00:04:16,660
And that is it.

55
00:04:16,690 --> 00:04:20,290
So here we pass in our connection string.

56
00:04:20,320 --> 00:04:23,770
Make sure you have your connections trained.

57
00:04:23,860 --> 00:04:29,710
So I'll copy my connection string here and let me come back to the project.

58
00:04:29,710 --> 00:04:31,750
And here I'll provide it.

59
00:04:31,750 --> 00:04:32,760
I'll start.

60
00:04:32,860 --> 00:04:38,560
And remember, it says that Mongoose David was asynchronous environment.

61
00:04:38,560 --> 00:04:43,500
So here it means that we need to handle both success and failure.

62
00:04:43,510 --> 00:04:50,360
It means when everything goes on where we want to tell the user that the connection was successful.

63
00:04:50,380 --> 00:04:52,990
For this one, I'm using promise.

64
00:04:52,990 --> 00:04:57,520
So here I'm going to console.log and it says that DB.

65
00:04:58,270 --> 00:04:59,320
Connect it.

66
00:05:00,380 --> 00:05:01,010
As that.

67
00:05:01,010 --> 00:05:02,570
So let me remove this.

68
00:05:03,290 --> 00:05:04,050
Column here.

69
00:05:04,070 --> 00:05:07,490
And if something goes wrong, I'll bring my catch.

70
00:05:07,490 --> 00:05:12,380
And in here I have the error and I can go ahead and console.log.

71
00:05:12,380 --> 00:05:13,250
The.

72
00:05:14,250 --> 00:05:20,850
Error dot message and we have a message on that and that is it.

73
00:05:20,880 --> 00:05:26,530
You see, I'm done with a connection as compared to MongoDB.

74
00:05:26,610 --> 00:05:29,940
You can see how we're able to connect using this function.

75
00:05:29,940 --> 00:05:34,790
So now how can we text that our connection was successful?

76
00:05:34,800 --> 00:05:37,220
So back to my terminal here.

77
00:05:37,320 --> 00:05:40,860
Make use of node and the file was Mongoose.

78
00:05:40,860 --> 00:05:41,910
So hit enter.

79
00:05:41,940 --> 00:05:43,440
Now let's wait.

80
00:05:44,910 --> 00:05:50,830
And MongoDB has been connected or DB has connected.

81
00:05:50,850 --> 00:05:59,580
So if I try to change the password and let's see if we are going to get some error now let me run the

82
00:05:59,580 --> 00:06:06,180
file again and let's see if you see that I get something called undefined.

83
00:06:06,330 --> 00:06:12,750
So I think there is something wrong or I forgot to bring the E here message.

84
00:06:13,170 --> 00:06:15,990
All right, but on my side, let me run it.

85
00:06:16,200 --> 00:06:19,920
You can see that we got bad of many bad authentication.

86
00:06:19,920 --> 00:06:21,690
That is, authentication failed.

87
00:06:21,720 --> 00:06:25,050
Meaning that I provided wrong password.

88
00:06:25,080 --> 00:06:27,090
So that is it.

89
00:06:27,270 --> 00:06:32,880
So this is how we can connect to MongoDB using Mongoose in the easiest way.

90
00:06:33,150 --> 00:06:36,720
Now that we are done with connection in this video.

91
00:06:36,870 --> 00:06:42,230
Let's see how we can create record and save it into MongoDB.

