1
00:00:07,730 --> 00:00:08,930
In this video.

2
00:00:08,930 --> 00:00:12,530
Let's talk about mongoose schema.

3
00:00:12,710 --> 00:00:16,040
What is it and why do we care about it?

4
00:00:16,700 --> 00:00:22,520
In short, schema simply means a way of creating document.

5
00:00:22,970 --> 00:00:27,290
So it's a blueprint of creating document.

6
00:00:27,380 --> 00:00:36,470
So looking at this picture here, you can see that we have the mother who is what, directing his or

7
00:00:36,470 --> 00:00:37,970
her children here.

8
00:00:38,510 --> 00:00:44,750
So the mother will decide that this is how I want my kids to behave.

9
00:00:45,260 --> 00:00:48,310
So it's a way of world creating documents.

10
00:00:48,320 --> 00:00:53,720
So these children are called the document when it comes to MongoDB.

11
00:00:53,870 --> 00:00:56,540
So let's see more about schema.

12
00:00:56,540 --> 00:00:57,220
In that way.

13
00:00:57,230 --> 00:00:59,750
You better understand what it is.

14
00:01:00,230 --> 00:01:10,640
The first point under schema is that it defines the structure of documents in a MongoDB collection.

15
00:01:11,300 --> 00:01:12,380
Dickey time.

16
00:01:12,380 --> 00:01:17,660
Here is what we call the structure of document.

17
00:01:17,780 --> 00:01:25,580
When we say structure of documents is simply means that the way you want your document or your record

18
00:01:25,580 --> 00:01:26,390
to be.

19
00:01:26,870 --> 00:01:35,450
So let's say that we are creating a block application and as a developer you are going to decide what

20
00:01:35,450 --> 00:01:40,010
kind of fields you want to be use in your blog application.

21
00:01:40,100 --> 00:01:47,300
So let's say for a blog, you want a user to provide a title of the blog, description of the blog,

22
00:01:47,300 --> 00:01:50,870
and provide image of the post or the blog.

23
00:01:50,900 --> 00:01:55,020
So this what you call this structure the way you want it.

24
00:01:55,040 --> 00:02:02,720
But when we are working with MongoDB alone, you could see that there is no structure here, there is

25
00:02:02,720 --> 00:02:03,740
no structure.

26
00:02:03,860 --> 00:02:12,170
Anybody can send any data into MongoDB as soon as the person provide the property here inside it is

27
00:02:12,170 --> 00:02:13,490
going to create.

28
00:02:13,820 --> 00:02:17,570
But with Mongoose you are going to have a way structured.

29
00:02:17,870 --> 00:02:24,710
So like I said, if you are using MongoDB like this one and you want to accept a specific field, unless

30
00:02:24,710 --> 00:02:29,030
we implement that validation, which is more work to do.

31
00:02:29,210 --> 00:02:38,120
So for MongoDB, like this one, I can go ahead and insert any property here or whatever and I can send

32
00:02:38,120 --> 00:02:39,940
any value into that when I save.

33
00:02:39,950 --> 00:02:45,950
It is going to create this field for me, even though as a developer I didn't state that.

34
00:02:45,950 --> 00:02:52,520
But as soon as a user provides, this is going to insert and we can avoid this one when you provide

35
00:02:52,520 --> 00:02:54,140
our custom validation.

36
00:02:54,260 --> 00:03:02,750
But with Mongoose, we have this structured we are going to decide that we want this field or this field

37
00:03:02,870 --> 00:03:07,060
in the field which is not specified inside our schema.

38
00:03:07,070 --> 00:03:09,990
We are going to throw an error to the user.

39
00:03:10,040 --> 00:03:12,530
That's how schema is.

40
00:03:12,530 --> 00:03:22,210
Or about the next point by schema is that we specify the data types for each field in a document.

41
00:03:22,220 --> 00:03:27,440
So this means that we can specify any kind of data type.

42
00:03:27,440 --> 00:03:35,030
For example, if you want to create a post, you have a title which is a string, let's say image a

43
00:03:35,030 --> 00:03:35,810
string.

44
00:03:35,840 --> 00:03:43,010
Let's see what's going to be array of tags and let's say a number of posts can be a number and we can

45
00:03:43,010 --> 00:03:45,620
achieve that using Mongoose.

46
00:03:45,620 --> 00:03:54,290
But with MongoDB, I can say that the field for a student here, let's say a mount, and when I enter

47
00:03:54,290 --> 00:04:01,970
it I can say true this a number, but when I create it I added a boolean value as a value which is not

48
00:04:01,970 --> 00:04:02,720
ideal.

49
00:04:02,750 --> 00:04:05,180
That is why Mongoose is awesome.

50
00:04:05,450 --> 00:04:13,040
The next point is that it validates the commands before they are inserted in a collection, as we discussed

51
00:04:13,040 --> 00:04:14,240
about Mongoose.

52
00:04:14,420 --> 00:04:19,040
The next point is that it specify default values for fields.

53
00:04:19,040 --> 00:04:26,930
So let's say that if you want to create a record, we can provide a default value, that if the user

54
00:04:26,930 --> 00:04:32,120
don't provide that value, you want to give a default value for a particular field.

55
00:04:32,450 --> 00:04:37,820
So let's say here we can provide a field says it's merit.

56
00:04:38,300 --> 00:04:43,100
So for this value, sometimes it's more private and most users don't want to provide.

57
00:04:43,100 --> 00:04:50,210
So by default we can say that by default, if a user don't provide, you will see false or true and

58
00:04:50,210 --> 00:04:54,170
we can achieve that inside mongoose code schema.

59
00:04:54,410 --> 00:05:00,470
And next is is specify other options such as whether a field is required.

60
00:05:00,470 --> 00:05:03,320
And that is what I said about validation.

61
00:05:03,440 --> 00:05:11,330
So let's say that we have this document that we want to inject into MongoDB and we have specified a

62
00:05:11,330 --> 00:05:14,240
structure that a name is required.

63
00:05:14,240 --> 00:05:21,290
But when a user is trying to insert, the user may forgot or not even to provide the value for this

64
00:05:21,290 --> 00:05:21,860
field.

65
00:05:21,860 --> 00:05:26,870
So if the user is trying to save it, the application is going to throw an error.

66
00:05:26,870 --> 00:05:30,100
In that way the document cannot be saved.

67
00:05:30,110 --> 00:05:31,460
That is awesome.

68
00:05:32,210 --> 00:05:37,700
So before we end up this video, let's talk about model.

69
00:05:37,730 --> 00:05:40,040
What is a model?

70
00:05:40,080 --> 00:05:45,560
Well, models are instance of a schema.

71
00:05:45,980 --> 00:05:53,510
So what comes back after we have created our schema is what we call the model.

72
00:05:53,690 --> 00:06:04,310
So we can say that the schema is a class of which we can create an instance of object out of the schema.

73
00:06:04,490 --> 00:06:09,230
So we call this one the first one as the schema.

74
00:06:09,740 --> 00:06:10,790
And then.

75
00:06:11,080 --> 00:06:14,860
Schema, we can create instances of the schema.

76
00:06:15,130 --> 00:06:21,160
So let's say that the name of a schema is person, schema person.

77
00:06:21,790 --> 00:06:25,730
So we can create many persons out from this schema.

78
00:06:25,750 --> 00:06:30,670
We can have person one, person two, and person three has that.

79
00:06:30,970 --> 00:06:34,550
So a model is an instance of a schema.

80
00:06:34,570 --> 00:06:37,150
So with that schema, there will be no model.

81
00:06:37,180 --> 00:06:41,890
So it's a model that is going to help us to interact with MongoDB.

82
00:06:42,070 --> 00:06:45,300
For example, you can have a model for users.

83
00:06:45,310 --> 00:06:52,000
So if we want to find all users, we are not going to interact with the schema, but instead we are

84
00:06:52,000 --> 00:06:54,130
going to interact with the model.

85
00:06:54,610 --> 00:06:57,400
This is what we call collection in MongoDB.

86
00:06:57,430 --> 00:06:57,910
Right.

87
00:06:57,910 --> 00:07:01,930
And on that we can have all the method like create on that.

88
00:07:02,140 --> 00:07:05,200
But with Mongoose, we call this one model.

89
00:07:05,350 --> 00:07:10,690
And on that, I'm going to have all the methods for Mongoose or MongoDB to interact.

90
00:07:10,720 --> 00:07:16,170
The cool part is that we can use Mongoose method like find delete.

91
00:07:16,180 --> 00:07:20,080
Likewise in the MongoDB method also.

92
00:07:20,110 --> 00:07:21,580
How awesome it is.

93
00:07:21,580 --> 00:07:26,440
That's why Mongoose extends the functionality of Mongo.

94
00:07:26,440 --> 00:07:33,490
DB Now that we have all the concepts done in this video, let's go ahead and create application and

95
00:07:33,490 --> 00:07:37,540
start working with Mongoose in the next video.

