1
00:00:03,710 --> 00:00:10,380
Now that we have had a quick overview of Mongoose ODM and

2
00:00:10,380 --> 00:00:16,710
how Mongoose brings schemas to structure the documents that we store in our MongoDB,

3
00:00:16,710 --> 00:00:19,830
let's look at how we make use of

4
00:00:19,830 --> 00:00:27,000
the Mongoose node module and then incorporate it into our node application.

5
00:00:27,000 --> 00:00:31,065
Then use Mongoose to communicate with

6
00:00:31,065 --> 00:00:35,760
our MongoDB server and then store and retrieve

7
00:00:35,760 --> 00:00:41,355
documents with structure information in the documents.

8
00:00:41,355 --> 00:00:44,830
To get started on this exercise,

9
00:00:44,830 --> 00:00:48,080
go to a convenient location on your computer.

10
00:00:48,080 --> 00:00:52,760
So, here I am in the Node JS folder where I have been storing

11
00:00:52,760 --> 00:00:57,020
all the examples from this course and then I'm going to

12
00:00:57,020 --> 00:01:02,210
create a new folder named node Mongoose.

13
00:01:02,210 --> 00:01:10,320
This is where we will create the node application using Mongoose next.

14
00:01:10,320 --> 00:01:12,900
In the terminal or the command window,

15
00:01:12,900 --> 00:01:19,080
move to the node Mongoose folder and then initialize a node application there.

16
00:01:19,080 --> 00:01:22,385
So, let me type npm init and then

17
00:01:22,385 --> 00:01:26,359
initialize the node application and then I would name the application

18
00:01:26,359 --> 00:01:34,174
as node-Mongoose and the entry point is index.js git history,

19
00:01:34,174 --> 00:01:42,590
keywords, author and license and we'll say okay.

20
00:01:42,590 --> 00:01:49,574
Once your node package.json file is created,

21
00:01:49,574 --> 00:01:54,500
open the project in the text editor of your choice.

22
00:01:54,500 --> 00:02:02,809
So, here I have my project open in Visual Studio.

23
00:02:02,809 --> 00:02:05,915
Let me go into the package.json file and then add in

24
00:02:05,915 --> 00:02:14,750
the startup script and then we'll

25
00:02:14,750 --> 00:02:24,160
say start node index as usual whenever we start out with a new node application.

26
00:02:24,160 --> 00:02:29,945
Then, going to the terminal or the command window, let's install Mongoose.

27
00:02:29,945 --> 00:02:37,290
So, at the prompt type npm install Mongoose, save.

28
00:02:39,470 --> 00:02:45,570
Which at the moment is Mongoose 5.1.7.

29
00:02:45,570 --> 00:02:49,760
So, these are the versions that I'm going to be using in this course.

30
00:02:49,760 --> 00:02:54,920
Then going to my application in the editor.

31
00:02:54,920 --> 00:03:01,430
Let me create a sub folder in the project called as Models.

32
00:03:01,430 --> 00:03:07,320
So, this is where I'm going to be storing all the models for my application.

33
00:03:07,340 --> 00:03:10,230
In the models folder,

34
00:03:10,230 --> 00:03:15,170
let me create a new file named dishes.js.

35
00:03:15,170 --> 00:03:19,535
So, this is where I'm going to create the schema and the model

36
00:03:19,535 --> 00:03:24,680
for my dishes document that I'm going to store,

37
00:03:24,680 --> 00:03:29,770
the dishes collection which stores the documents for each dishes.

38
00:03:29,770 --> 00:03:36,730
So, right there, let me first import Mongoose.

39
00:03:39,320 --> 00:03:47,880
So, we'll require Mongoose in this file and then say

40
00:03:47,880 --> 00:03:57,940
const schema is Mongoose schema.

41
00:03:59,420 --> 00:04:03,510
Then, we'll create the schema here.

42
00:04:03,510 --> 00:04:13,845
So, we'll say const dishSchema new Schema.

43
00:04:13,845 --> 00:04:19,590
So, this is where I define the schema for my dish.

44
00:04:20,830 --> 00:04:27,270
Inside this schema, let me define the various values.

45
00:04:27,270 --> 00:04:29,830
So, I'll say name,

46
00:04:29,890 --> 00:04:34,405
which is the type String.

47
00:04:34,405 --> 00:04:41,480
So, this is the Schema type and then this I would declare as required.

48
00:04:41,480 --> 00:04:43,460
So, I'll say true.

49
00:04:43,460 --> 00:04:52,790
So, every document will have the name as a required field there and then I

50
00:04:52,790 --> 00:04:57,185
will also declare this as unique meaning that

51
00:04:57,185 --> 00:05:02,545
no two documents should have the same name field in there.

52
00:05:02,545 --> 00:05:08,230
So, that is the first field in my document.

53
00:05:10,190 --> 00:05:19,440
Then, we will also include another field called as, description.

54
00:05:19,440 --> 00:05:29,205
The description is of the type string and required true.

55
00:05:29,205 --> 00:05:35,165
Also, we can have Mongoose automatically insert

56
00:05:35,165 --> 00:05:41,639
timestamps into our model.

57
00:05:41,639 --> 00:05:50,340
So, right there we can just set up the flag time stamps: true.

58
00:05:50,340 --> 00:05:55,810
So, this will automatically add the created at and updated at,

59
00:05:55,810 --> 00:06:01,340
two timestamps into each document that is stored

60
00:06:01,340 --> 00:06:07,235
in our application and it'll automatically update these values.

61
00:06:07,235 --> 00:06:11,330
Whenever we update the document and the created at will be automatically

62
00:06:11,330 --> 00:06:15,390
initialized when the document is first creator of this time.

63
00:06:15,390 --> 00:06:22,680
After this, we will say var Dishes.

64
00:06:22,680 --> 00:06:24,549
So, given the schema,

65
00:06:24,549 --> 00:06:29,605
now we going to construct the model from this schema.

66
00:06:29,605 --> 00:06:35,215
So, we'll say Mongoose model and dish,

67
00:06:35,215 --> 00:06:43,570
and this is going to be using the dish schema that we have just declared earlier.

68
00:06:43,570 --> 00:06:48,590
Then we will export

69
00:06:48,590 --> 00:06:55,725
this model from this file here.

70
00:06:55,725 --> 00:06:58,410
So, we'll say module export dishes.

71
00:06:58,410 --> 00:07:00,545
So, now we have constructed

72
00:07:00,545 --> 00:07:05,780
the Mongoose schema and the corresponding model and the model is now exported from here.

73
00:07:05,780 --> 00:07:10,580
So, this can be imported in our application and used.

74
00:07:10,580 --> 00:07:13,345
So, within our project folder,

75
00:07:13,345 --> 00:07:20,415
let me create a new file named index.js.

76
00:07:20,415 --> 00:07:23,265
In the index.js file,

77
00:07:23,265 --> 00:07:31,450
let me first require Mongoose,

78
00:07:31,820 --> 00:07:34,900
and then after this we can say

79
00:07:34,900 --> 00:07:44,970
const Dishes require models dishes.

80
00:07:44,970 --> 00:07:49,210
Because we have already created the Dishes model in

81
00:07:49,210 --> 00:07:55,740
the models dishes file there and then we are requiring it here.

82
00:07:55,740 --> 00:08:02,975
Now, to establish the connection to the Mongo server,

83
00:08:02,975 --> 00:08:09,330
we'll say const URL mongodb.

84
00:08:09,840 --> 00:08:17,110
Localhost 27017, conFusion.

85
00:08:17,110 --> 00:08:25,760
So, this database is the one that we will connect to from our application and

86
00:08:25,760 --> 00:08:35,125
then after that we can establish the connection by saying const connect Mongoose.

87
00:08:35,125 --> 00:08:38,195
Mongoose supports this connect method,

88
00:08:38,195 --> 00:08:41,695
which takes the URL as the first parameter.

89
00:08:41,695 --> 00:08:44,250
So once we have established this,

90
00:08:44,250 --> 00:08:49,470
then we simply say connect then.

91
00:08:49,470 --> 00:08:52,280
Note that since we are already using promises,

92
00:08:52,280 --> 00:08:57,680
we can just say connect then and then this will take

93
00:08:57,680 --> 00:09:03,774
a function as the parameter

94
00:09:03,774 --> 00:09:10,175
and inside here we can now connect to the database.

95
00:09:10,175 --> 00:09:13,550
So now once this connection is established,

96
00:09:13,550 --> 00:09:17,800
let me do a console log

97
00:09:17,800 --> 00:09:26,060
saying connected correctly to the server,

98
00:09:26,100 --> 00:09:36,355
and here I can create a new Dish by saying var

99
00:09:36,355 --> 00:09:45,560
new Dish and then Dishes and inside here I can specify a document so I'm creating

100
00:09:45,560 --> 00:09:50,170
a new Dish of this kind and then so in here I will

101
00:09:50,170 --> 00:09:55,510
specify the two fields that are required.

102
00:09:55,510 --> 00:09:57,920
So I'll say name,

103
00:10:07,950 --> 00:10:16,390
description and so this is how you would create a new Dish and then once we create

104
00:10:16,390 --> 00:10:24,910
a new Dish from the model we'll simply say new Dish save.

105
00:10:24,910 --> 00:10:29,760
The save method will cause this Dish value to be

106
00:10:29,760 --> 00:10:35,060
saved and then as you expect will return a promise and

107
00:10:35,060 --> 00:10:45,165
then in here we will get the Dish value as call back in there and then,I

108
00:10:45,165 --> 00:10:54,940
can simply log the Dish value here and then after that we'll say

109
00:10:54,940 --> 00:11:00,915
Dishes find and so we'll find

110
00:11:00,915 --> 00:11:07,090
all the Dishes and then say exec.

111
00:11:07,090 --> 00:11:10,255
The exec will ensure that this is

112
00:11:10,255 --> 00:11:15,250
executed and that it will return a promise and so that promise will

113
00:11:15,250 --> 00:11:19,660
be returned so that it can then

114
00:11:19,660 --> 00:11:27,040
chain the method to the remaining ones.

115
00:11:27,040 --> 00:11:33,975
So you see how I am using promises and then I am invoking the previous method so this one

116
00:11:33,975 --> 00:11:41,395
finds all the Dishes within my database,

117
00:11:41,395 --> 00:11:45,905
in the Dishes collection and then makes it available to me.

118
00:11:45,905 --> 00:11:55,550
So when I get the Dishes then I can just console log the Dishes just to see what is

119
00:11:55,550 --> 00:11:58,790
returned and this obviously at this stage should

120
00:11:58,790 --> 00:12:02,710
return this one single Dish that I've inserted into

121
00:12:02,710 --> 00:12:10,940
my Dishes collection and then we will say

122
00:12:11,010 --> 00:12:15,550
return Dishes remove with

123
00:12:15,550 --> 00:12:19,930
an empty JavaScript object which will remove all the Dishes from

124
00:12:19,930 --> 00:12:24,384
the database and return

125
00:12:24,384 --> 00:12:31,790
mongoose connection close which closes the connection to the database.

126
00:12:35,040 --> 00:12:38,830
And catch any error at this point.

127
00:12:38,830 --> 00:12:49,430
So we'll use the catch and then console log.

128
00:12:50,600 --> 00:12:57,820
That's it. So what we are doing here is we're creating the new this Dish and then we are

129
00:12:57,820 --> 00:13:04,255
saving the Dish then we are finding all the Dishes from the collection.

130
00:13:04,255 --> 00:13:06,740
That's it. We are using promises here,

131
00:13:06,740 --> 00:13:10,435
so we have chained all the dens in here. That's it.

132
00:13:10,435 --> 00:13:15,595
Let's save the changes and then see this application executing.

133
00:13:15,595 --> 00:13:17,455
Go into to the terminal,

134
00:13:17,455 --> 00:13:21,505
at the prompt type npm start and you will

135
00:13:21,505 --> 00:13:25,735
see that my node application runs and then it shows,

136
00:13:25,735 --> 00:13:33,110
first, that it has created this particular Dish.

137
00:13:33,110 --> 00:13:37,285
Note in particular, that these two fields,

138
00:13:37,285 --> 00:13:39,365
updated at and created at,

139
00:13:39,365 --> 00:13:43,465
these two timestamps have automatically been added into

140
00:13:43,465 --> 00:13:47,790
the Dish here and so you can see that the created at and

141
00:13:47,790 --> 00:13:51,630
updated at timestamps are exactly the same at the moment and

142
00:13:51,630 --> 00:13:57,235
then the ID is also automatically added in.

143
00:13:57,235 --> 00:14:03,210
In the next step we are printing out all the Dishes that have been retrieved and so you

144
00:14:03,210 --> 00:14:10,340
can see that this one single Dish is in the collection,

145
00:14:10,340 --> 00:14:15,935
Dishes collection, and that is what is printed out on the screen here.

146
00:14:15,935 --> 00:14:25,660
Let's now initialize the Git repository and then let's set up the got gitignore file

147
00:14:25,660 --> 00:14:35,125
so going to the editor then we create the.gitignore file and then add

148
00:14:35,125 --> 00:14:40,240
node modules to the gitignore file and save the changes and

149
00:14:40,240 --> 00:14:45,325
then going back to the prompt let me

150
00:14:45,325 --> 00:14:48,550
type git status and we see that

151
00:14:48,550 --> 00:14:51,955
we have these files that have not been checked in so we'll say

152
00:14:51,955 --> 00:14:56,545
git add and then git

153
00:14:56,545 --> 00:15:04,535
commit the message Mongoose Part One.

154
00:15:04,535 --> 00:15:08,110
With this we complete this exercise.

155
00:15:08,110 --> 00:15:14,300
In this exercise we have seen how easy it is to set up a node application with

156
00:15:14,300 --> 00:15:21,335
mongoose and connect to our MongoDB server and then interact with it.

157
00:15:21,335 --> 00:15:24,985
Now since mongoose builds upon the MongoDB server,

158
00:15:24,985 --> 00:15:28,980
Mongoose can access all the various methods

159
00:15:28,980 --> 00:15:32,690
that are supported by the MongoDB driver also.