1
00:00:00,330 --> 00:00:08,130
Beautiful, and once our connection string is a variable, now let's set up the structure for our future

2
00:00:08,130 --> 00:00:16,440
documents and assign them to the collection and we're going to do that using schema and model from the

3
00:00:16,440 --> 00:00:16,880
Mongo's.

4
00:00:17,460 --> 00:00:23,150
And the way is going to look like we're going to go to our star and we'll create yet another folder.

5
00:00:23,490 --> 00:00:25,860
And in this case, we'll call these models.

6
00:00:26,190 --> 00:00:32,930
And instead of the models, we create a new file and we'll call this task Jass.

7
00:00:33,230 --> 00:00:35,460
So we're going to go here with Task James.

8
00:00:35,700 --> 00:00:41,410
And then the setup is going to be following where, again, we import Mangoush and we set it equal to

9
00:00:41,410 --> 00:00:48,630
require and Mongo's, of course, and then we're looking for Mongo's, that schema.

10
00:00:48,990 --> 00:00:56,690
So we set up a new variable and I'll call this task schema schema, and that is equal to new.

11
00:00:57,000 --> 00:01:02,490
So we're using the constructor and we go with new mongoose and schema.

12
00:01:02,970 --> 00:01:06,870
And you're probably wondering, OK, what on earth is happening?

13
00:01:07,440 --> 00:01:15,270
Well, if you remember when we were setting up the atlas manually, one of the first things that I mentioned

14
00:01:15,630 --> 00:01:20,720
is the fact that for our document, there is no set structure.

15
00:01:21,300 --> 00:01:27,530
But that's exactly what I want, where for my tasks, I only want the name, which is going to be a

16
00:01:27,540 --> 00:01:33,840
string and a completed property, which is going to be a boolean, because you would have to agree that

17
00:01:33,840 --> 00:01:41,670
it's not going to make much sense if somewhere in my tasks I'll have an array with the color values.

18
00:01:42,030 --> 00:01:42,460
Correct.

19
00:01:42,780 --> 00:01:49,100
So therefore we go here we go with Mongo's schema and using schema, we'll set up the structure for

20
00:01:49,140 --> 00:01:53,790
all the documents that eventually we'll have in our collection.

21
00:01:54,120 --> 00:02:01,050
And the syntax is following where again, we use key value pairs and eventually will set them equal

22
00:02:01,050 --> 00:02:04,410
to an object and we'll pass in more options.

23
00:02:04,740 --> 00:02:11,820
But since I just want to get something quickly in our database, we'll just start quick and dirty where

24
00:02:11,820 --> 00:02:14,690
essentially I want to come up with a key in my case.

25
00:02:14,700 --> 00:02:17,740
Again, that is going to be name and completed.

26
00:02:17,880 --> 00:02:21,770
So those are going to be the two keys and I'll just set up two types.

27
00:02:22,170 --> 00:02:28,420
So that is a bare minimum where you go with the key name and then the actual schema type.

28
00:02:28,620 --> 00:02:30,870
So what type of data this is going to be?

29
00:02:31,170 --> 00:02:37,410
And if you're interested on what options we have to navigate to Mongo's official docs and then look

30
00:02:37,410 --> 00:02:43,440
for schema types, keep on scrolling and then of course you can see string number and yada, yada,

31
00:02:43,440 --> 00:02:43,820
yada.

32
00:02:44,190 --> 00:02:46,870
So navigate back and I'll say name.

33
00:02:47,250 --> 00:02:52,410
So that's going to be my first key and I'll just set it equal to string one comma.

34
00:02:52,680 --> 00:02:55,020
And of course I'm looking for completed.

35
00:02:55,530 --> 00:02:56,100
Completed.

36
00:02:56,100 --> 00:03:01,110
That is going to be my second key and that won't be set equal to Boolean again.

37
00:03:01,320 --> 00:03:07,470
Barebone setup will do more complicated a little bit later and then once we have the schema.

38
00:03:07,920 --> 00:03:14,850
So essentially once we have the structure for the data, now we want to set up that model and think

39
00:03:14,850 --> 00:03:19,180
of model as a representation for the collection.

40
00:03:19,800 --> 00:03:22,500
So these guys are sticking in a product.

41
00:03:22,770 --> 00:03:31,830
So now of course, will create tasks collection and then all the tasks that we are about to push into

42
00:03:31,830 --> 00:03:35,790
our database, of course, will be added to that specific collection.

43
00:03:36,060 --> 00:03:40,950
And what's even more cool in Mongo's, a model is a wrapper for a schema.

44
00:03:41,560 --> 00:03:48,870
So if the schema defines the structure for the document, like the type validations and etc., a Mongo's

45
00:03:48,870 --> 00:03:51,650
model provides an interface to the database.

46
00:03:51,960 --> 00:03:59,370
So using the model will be able to create, update, query and delete our documents with great ease

47
00:03:59,640 --> 00:04:05,940
since the API is extremely, extremely straightforward and you'll see what I mean in the upcoming videos.

48
00:04:06,570 --> 00:04:10,740
And we do that by creating a model and we write away export.

49
00:04:11,010 --> 00:04:17,519
So go with module DOT exports and that is equal to Mongo's van model.

50
00:04:17,940 --> 00:04:20,130
And the model is looking for two things.

51
00:04:20,399 --> 00:04:24,630
It is looking for the name and then we want to pass in the schema.

52
00:04:24,960 --> 00:04:27,840
So in here I'm going to go with task and then comma.

53
00:04:28,050 --> 00:04:33,750
And of course this is the schema that I want to pass in so say here task schema.

54
00:04:33,930 --> 00:04:41,190
And once we're done with this, the only thing we need to do is go to our controllers and start using

55
00:04:41,190 --> 00:04:41,910
our model.

