1
00:00:08,100 --> 00:00:13,200
Welcome to brand new section, and that is Mongoose.

2
00:00:13,770 --> 00:00:19,710
Well, if you are new to web development, this will be your state.

3
00:00:20,370 --> 00:00:26,880
You may be thinking that are we going to learn a new programming language or a new database?

4
00:00:26,910 --> 00:00:29,910
The answer is no.

5
00:00:30,060 --> 00:00:36,190
But instead, it is going to make our life easier as web developers.

6
00:00:36,210 --> 00:00:38,950
So what is Mongoose?

7
00:00:38,970 --> 00:00:49,440
So let's understand that Mongoose is a Node.js module that provides an object data modeling called ODM

8
00:00:49,440 --> 00:00:52,290
interface for MongoDB.

9
00:00:52,620 --> 00:00:59,550
So it simply means that this is a tool for us to interact with MongoDB.

10
00:00:59,580 --> 00:01:01,410
Pretty easy.

11
00:01:01,920 --> 00:01:09,240
So if you remember, after we learned what is called Node.js, then we learned what is called Express

12
00:01:09,240 --> 00:01:11,940
JS and we saw the differences.

13
00:01:12,170 --> 00:01:19,260
Express JS Help us to write Node.js code or service or application in the easiest way.

14
00:01:19,590 --> 00:01:25,350
So Mongoose help us to work with MongoDB in the easiest way.

15
00:01:25,380 --> 00:01:28,860
So what is Mongoose and not share?

16
00:01:29,040 --> 00:01:35,160
Well, like I said, Mongoose was built on top of MongoDB.

17
00:01:35,190 --> 00:01:41,520
So if you write Mongoose code, indirectly we are running MongoDB code.

18
00:01:41,730 --> 00:01:49,920
So in a nutshell, Mongoose help us to interact with MongoDB in the easiest way.

19
00:01:50,070 --> 00:01:52,950
So why Mongoose in that way?

20
00:01:52,950 --> 00:01:56,820
You better understand why we need mongoose.

21
00:01:56,970 --> 00:02:03,560
Well, the first point is that it is the easiest way to model data.

22
00:02:03,570 --> 00:02:08,070
And we have dedicated video to talk about data modeling.

23
00:02:08,400 --> 00:02:17,460
For example, let's say that we have a block application where a user can create a post and a user can

24
00:02:17,460 --> 00:02:18,810
create a comment.

25
00:02:19,050 --> 00:02:25,450
So let's say that we want to find a post that has been created by a particular user.

26
00:02:25,470 --> 00:02:27,180
How are we going to do it?

27
00:02:27,390 --> 00:02:34,950
So if we are using MongoDB to get this logic done, we need to play around with MongoDB a lot, but

28
00:02:34,950 --> 00:02:39,270
with Mongoose is going to help us to model data easily.

29
00:02:39,390 --> 00:02:46,470
And after that, it is also the easiest way to access MongoDB collections.

30
00:02:46,860 --> 00:02:48,090
Do you remember?

31
00:02:48,090 --> 00:02:58,800
And MongoDB to be able to interact with the collection first unless we install the MongoDB drivers and

32
00:02:58,800 --> 00:03:04,800
next is we connect and next is we select the database and then the collection.

33
00:03:04,800 --> 00:03:08,910
And after that we can go ahead and interact with the collection.

34
00:03:09,270 --> 00:03:15,780
But with Mongoose, you're going to be pretty easy to get that one done and we will see as we move on.

35
00:03:16,080 --> 00:03:24,180
The next step is that it validate documents before they are inserted into a collection.

36
00:03:24,480 --> 00:03:31,830
So looking at this one, you can see that for this one to be able to implement validation on less,

37
00:03:31,830 --> 00:03:33,420
we do it by ourselves.

38
00:03:33,810 --> 00:03:38,010
So let's say that we are implementing this one in a route.

39
00:03:38,010 --> 00:03:42,030
So always we need to check the payload that is coming in.

40
00:03:42,150 --> 00:03:49,140
If the title of a certain post is more than this or it's an empty, then we can catch the error before

41
00:03:49,140 --> 00:03:51,150
it gets to the MongoDB.

42
00:03:51,480 --> 00:03:55,920
But with Mongoose, it's going to be pretty easy to get that.

43
00:03:55,980 --> 00:03:56,640
And then.

44
00:03:56,640 --> 00:03:58,470
So let's get going.

45
00:03:58,830 --> 00:04:08,430
The next step is it provides advanced custom data types or plugins, meaning that we can extend mongoose

46
00:04:08,430 --> 00:04:09,480
functionality.

47
00:04:09,630 --> 00:04:15,290
So behind the scene we are extending the MongoDB functionality.

48
00:04:15,300 --> 00:04:23,940
We can add our custom method, which is not part of the core method that has been shipped in with MongoDB.

49
00:04:24,090 --> 00:04:29,960
So the next point is that it is the most popular ODM libraries for Node.js.

50
00:04:29,970 --> 00:04:34,170
So ODM simply means object document model.

51
00:04:34,410 --> 00:04:41,250
And it will see that when we get into Mongoose, you can see the number of downloads per week and it

52
00:04:41,250 --> 00:04:43,200
has large community.

53
00:04:43,200 --> 00:04:47,930
So in case you face any bug you're going to find easy on the internet.

54
00:04:47,940 --> 00:04:50,760
That's why Mongoose is awesome.

55
00:04:50,910 --> 00:04:59,850
But before you understand Mongoose, unless you understand two technical term core schema and model,

56
00:05:00,030 --> 00:05:01,140
then this video.

57
00:05:01,140 --> 00:05:02,850
Let's talk about schema.

