1
00:00:06,820 --> 00:00:08,200
Welcome back.

2
00:00:08,620 --> 00:00:17,160
This is the time that is we are going to create record and save into MongoDB using Mongoose.

3
00:00:17,210 --> 00:00:21,970
So let me have beautiful comment here and says that great students.

4
00:00:24,130 --> 00:00:28,030
All right, so now we need to learn mongoose method.

5
00:00:28,150 --> 00:00:36,400
So, like I said, we can use any MongoDB method, for example, insect fine and many more.

6
00:00:36,580 --> 00:00:39,860
And we can also use mongoose method.

7
00:00:39,880 --> 00:00:47,800
So back to Mongoose website and now click on the and the API is click on query.

8
00:00:47,950 --> 00:00:52,410
And here we are going to see all the query method we can use.

9
00:00:52,420 --> 00:00:54,610
You can see a bunch of them.

10
00:00:54,640 --> 00:00:57,790
We see that is our sun.

11
00:00:57,910 --> 00:01:03,790
We can perform more operations using this mongoose query method.

12
00:01:03,940 --> 00:01:10,300
So you can see that if we scroll down for Mongoose, we have a couple of methods that's going to help

13
00:01:10,300 --> 00:01:14,950
us to insect data, and we have one called Create.

14
00:01:14,950 --> 00:01:19,060
So let's go down and let's locate what is called create.

15
00:01:19,060 --> 00:01:21,160
Sometimes the find is difficult.

16
00:01:21,160 --> 00:01:23,080
We can make a search for it.

17
00:01:23,080 --> 00:01:26,110
So let's see if I'm going to see create here.

18
00:01:26,770 --> 00:01:32,410
Let's have some patient and let's see create this delete count.

19
00:01:33,160 --> 00:01:34,160
Okay.

20
00:01:34,180 --> 00:01:36,400
Is nowhere to be found here.

21
00:01:36,430 --> 00:01:37,450
Maybe I can see.

22
00:01:37,450 --> 00:01:39,520
So let's search for create here.

23
00:01:39,970 --> 00:01:44,170
If we search for create now I'm going to see the method for creating.

24
00:01:44,930 --> 00:01:50,400
And you can see that we have AP High and then the model name called Create.

25
00:01:50,420 --> 00:01:59,480
So when I click on that, I'm going to see the format for creating and here we go model which represents

26
00:01:59,510 --> 00:02:03,590
the model that we have created here, which is this one.

27
00:02:03,770 --> 00:02:08,360
And on that we are going to use method for mongoose called create.

28
00:02:08,360 --> 00:02:09,870
And that is it.

29
00:02:09,889 --> 00:02:12,500
So let's look at the parameters it takes.

30
00:02:12,560 --> 00:02:20,390
First is it takes in the object and then document to insect so we can insert array that is to create

31
00:02:20,390 --> 00:02:22,730
money or a single object.

32
00:02:22,760 --> 00:02:29,940
And we can pass in additional option as that and then callback function and always it retains promise.

33
00:02:29,960 --> 00:02:34,490
So it means that we can use callback or primes or async await to get it done.

34
00:02:34,760 --> 00:02:40,790
So looking at the example here, they use a weight in front of the model and they use the method to

35
00:02:40,790 --> 00:02:43,310
create the parse in the details they want to create.

36
00:02:43,310 --> 00:02:46,920
And that is all and that is all.

37
00:02:46,940 --> 00:02:50,570
So let's see how we can achieve that inside our code.

38
00:02:50,600 --> 00:02:52,490
So here we go.

39
00:02:52,820 --> 00:02:56,920
So first is we bring in this student model.

40
00:02:56,930 --> 00:03:03,870
And on that we see I have create here and we can use insets from MongoDB.

41
00:03:03,890 --> 00:03:06,710
So let's use create as that.

42
00:03:06,710 --> 00:03:12,800
And for the create you pass in the details, we want to create as an object with key value per.

43
00:03:13,010 --> 00:03:16,110
And remember, we are going to follow this format.

44
00:03:16,130 --> 00:03:18,500
So the first one is name.

45
00:03:18,500 --> 00:03:21,590
So I will provide name as John.

46
00:03:21,590 --> 00:03:24,320
And then after that, the city.

47
00:03:25,750 --> 00:03:27,960
Going to say from Kumasi.

48
00:03:28,020 --> 00:03:34,520
And after that, the courses the person is going to take is going to be array of courses.

49
00:03:34,530 --> 00:03:38,850
So let's say computer science and let's say.

50
00:03:39,650 --> 00:03:40,730
English.

51
00:03:41,180 --> 00:03:47,720
And then let's say administration and let's maintain this one for now.

52
00:03:47,930 --> 00:03:53,390
After that, we need to provide the two properties is married and the age.

53
00:03:53,390 --> 00:03:56,330
So here we can provide is married.

54
00:03:56,720 --> 00:04:00,320
So let's provide true the person is married.

55
00:04:00,320 --> 00:04:04,540
And then the age of the students, let's say 20 years.

56
00:04:04,550 --> 00:04:07,280
So now we are done with it.

57
00:04:07,880 --> 00:04:15,050
So remember model always return a promise, meaning we need to await for success and failure.

58
00:04:15,170 --> 00:04:23,090
So we are going to use promise here, pass in the actual response student and I can cancel, log it

59
00:04:23,090 --> 00:04:27,950
and see whether it has been created or not as the student.

60
00:04:28,400 --> 00:04:35,930
And if something happens, we're going to catch it as an error pass in here and I'm going to get it

61
00:04:35,930 --> 00:04:39,230
as an error here console.log.

62
00:04:39,230 --> 00:04:41,810
The error as that.

63
00:04:41,810 --> 00:04:43,910
So now moment of truth.

64
00:04:44,210 --> 00:04:50,420
So let's go back and then restart our file, clear the terminal and let's run the file again.

65
00:04:50,420 --> 00:04:51,290
And let's see.

66
00:04:52,350 --> 00:05:00,930
A and you could see that it has been created for me successfully with name John city command C courses

67
00:05:00,930 --> 00:05:08,010
as that is merit age and I have two pairs for underscore ID and that's that.

68
00:05:08,040 --> 00:05:10,620
V So don't worry about this one.

69
00:05:10,620 --> 00:05:12,750
Let's focus on the ID.

70
00:05:13,140 --> 00:05:21,570
So let's go ahead and make sure that we see this data inside our MongoDB and we are going to use the

71
00:05:21,570 --> 00:05:23,840
compass to visualize it.

72
00:05:23,850 --> 00:05:27,030
So click on this icon to refresh.

73
00:05:27,030 --> 00:05:30,930
And next step is we need to locate our database.

74
00:05:30,930 --> 00:05:39,150
But remember, for Mongoose, when you are connecting or creating a record, we didn't specify the database

75
00:05:39,150 --> 00:05:43,680
name, so by default it will give us a test database.

76
00:05:43,680 --> 00:05:51,300
So if I click on that, we see that now I have the student record when I open it and indeed I'm going

77
00:05:51,300 --> 00:05:53,610
to see the student here.

78
00:05:53,850 --> 00:05:56,250
So how can we customize this one?

79
00:05:56,250 --> 00:06:03,960
So back to the connection string here where we connect, you can see that we have this you are error.

80
00:06:03,960 --> 00:06:09,780
And then first last question mark and then right, so here we go.

81
00:06:09,780 --> 00:06:14,850
So here we are going to specify the name of our database.

82
00:06:14,850 --> 00:06:17,400
So here we call it at school.

83
00:06:17,520 --> 00:06:18,330
That is it.

84
00:06:18,330 --> 00:06:20,700
So let's save this one.

85
00:06:20,910 --> 00:06:25,800
And then next step is let's go ahead and run the file again.

86
00:06:25,800 --> 00:06:30,150
We are going to create the same student and it has been created.

87
00:06:30,150 --> 00:06:32,700
We have duplicate uses, but don't worry.

88
00:06:32,700 --> 00:06:36,570
So let's get back to the compass and again, let's refresh it.

89
00:06:37,170 --> 00:06:42,510
And now I can see that instead of test we have database called school.

90
00:06:42,600 --> 00:06:50,040
So now let's remove the test one and let me type the name of my database called test and drop it and

91
00:06:50,040 --> 00:06:52,170
let's focus on the school.

92
00:06:52,170 --> 00:06:58,920
And indeed, under school we have one collection called Student and here we go.

93
00:06:58,950 --> 00:06:59,730
All right.

94
00:06:59,730 --> 00:07:05,820
That is all some guys and you can see that number of record here or the command, which is one.

95
00:07:06,090 --> 00:07:14,310
And we can go ahead and create more students by passing in different names, let's say Emmanuel and

96
00:07:14,310 --> 00:07:16,080
from Akra.

97
00:07:17,370 --> 00:07:18,750
And let's see.

98
00:07:18,750 --> 00:07:21,630
I like these courses and I'm not married.

99
00:07:22,380 --> 00:07:22,800
All right.

100
00:07:22,800 --> 00:07:27,060
So with this one, let's go ahead and then restart the file.

101
00:07:27,870 --> 00:07:28,740
The terminal.

102
00:07:28,740 --> 00:07:31,290
And let me run the file again.

103
00:07:31,620 --> 00:07:34,500
Let me see.

104
00:07:34,500 --> 00:07:35,190
Connected.

105
00:07:35,190 --> 00:07:37,080
And then record has happened.

106
00:07:37,080 --> 00:07:38,370
And here we go.

107
00:07:38,490 --> 00:07:41,220
Let me refresh my compass here.

108
00:07:41,220 --> 00:07:45,060
I'm going to see two students and indeed we have eight.

109
00:07:45,060 --> 00:07:48,300
And as you see the number of documents.

110
00:07:48,300 --> 00:07:56,880
So we can also create many students using create many or insect money so we can read more about the

111
00:07:56,880 --> 00:07:59,850
APIs which help us to get this one done.

112
00:08:00,060 --> 00:08:01,110
The next video.

113
00:08:01,110 --> 00:08:04,110
Let's see how we can fetch or record.

