1
00:00:00,210 --> 00:00:01,470
OK welcome back.

2
00:00:01,650 --> 00:00:07,830
So far we've seen how to interact with Mongo TV using the Mongo console which as I mentioned is nice

3
00:00:07,830 --> 00:00:12,510
for teaching things and for testing things out and debugging but it's not where we'll actually write

4
00:00:12,720 --> 00:00:17,250
the bulk of our code that interacts with the database where we'll actually be doing that is instead

5
00:00:17,250 --> 00:00:18,660
of our expressed code.

6
00:00:18,780 --> 00:00:23,290
So they pull up an example let's go to Yelp camp and just take a look at her app.

7
00:00:23,340 --> 00:00:24,110
Yes.

8
00:00:24,750 --> 00:00:27,720
Here's an example of where we would use mangu.

9
00:00:27,930 --> 00:00:35,340
So for instance here when we're adding a new campground to our array rather than adding campgrounds

10
00:00:35,340 --> 00:00:40,430
to an array you would have a database a mongo database and a collection called campgrounds.

11
00:00:40,530 --> 00:00:43,530
And this is actually coming up very soon in this series.

12
00:00:43,530 --> 00:00:48,570
And then when you submit a post request to slash campground rather than pushing into an array we're

13
00:00:48,570 --> 00:00:57,870
going to insert into the Mongar DP collection likewise appear on the get slashed campgrounds rather

14
00:00:57,870 --> 00:01:03,620
than just rendering campgrounds with the array campgrounds that's defined here.

15
00:01:03,690 --> 00:01:11,430
We're actually going to do a find a D-B campground find and then take the results of that and send that

16
00:01:11,430 --> 00:01:12,990
to the campground template.

17
00:01:13,550 --> 00:01:13,850
OK.

18
00:01:13,860 --> 00:01:15,150
So we'll get there.

19
00:01:15,450 --> 00:01:17,170
So that's what we're focusing on this lesson.

20
00:01:17,340 --> 00:01:21,000
And in order to do that we're going to learn about a tool called mongoose.

21
00:01:21,120 --> 00:01:26,060
So I have three main goals on it tell you what mongooses on explain why we're using it.

22
00:01:26,370 --> 00:01:30,000
And I also want to show you how to use it instead of a javascript file.

23
00:01:30,000 --> 00:01:32,450
Let's start by talking about what Mongoose is.

24
00:01:32,730 --> 00:01:34,790
So I have the official home page open.

25
00:01:34,920 --> 00:01:39,830
It says that Mongoose is an elegant Mongo DB object modeling for no genius.

26
00:01:40,120 --> 00:01:40,700
OK.

27
00:01:41,220 --> 00:01:42,750
Let's read a little bit more.

28
00:01:42,990 --> 00:01:47,610
Mongoose provides a straightforward schema based solution to model your application data.

29
00:01:47,720 --> 00:01:52,740
It includes built in typecasting validation queery building business logic coax and more out of the

30
00:01:52,740 --> 00:01:53,480
box.

31
00:01:53,550 --> 00:01:56,690
So I think this would make more sense to a seasoned developer.

32
00:01:56,790 --> 00:01:59,910
But what it really means is that Mongoose is a tool.

33
00:02:00,090 --> 00:02:05,970
It's a package that we're going to download with NPM that helps us interact with Mongo D-B instead of

34
00:02:05,970 --> 00:02:07,550
our javascript files.

35
00:02:07,650 --> 00:02:09,750
It is possible to do it without mongoose.

36
00:02:09,750 --> 00:02:14,280
There were other tools like this out there but it just makes it easier for us to interact with the database

37
00:02:14,520 --> 00:02:19,080
just like Jay Querrey makes it easier for us to interact with the Dom but we don't have to have a query

38
00:02:19,080 --> 00:02:19,380
.

39
00:02:19,530 --> 00:02:25,020
Mongoose makes it easier and cleaner for us to interact with the monkhood DB database but it's not necessary

40
00:02:25,020 --> 00:02:25,700
.

41
00:02:25,860 --> 00:02:30,860
So let's head back to cloud 9 and I'm going to go ahead and make a single javascript file.

42
00:02:31,020 --> 00:02:36,570
It's not going to be an express app it's just going to be a single file that I run with node node after

43
00:02:36,790 --> 00:02:42,640
Yes and when I run the file we want it to add something to our database using mongoose.

44
00:02:43,050 --> 00:02:44,110
So I'll get started.

45
00:02:44,110 --> 00:02:48,620
I'm inside of a directory that I made called databases and I'm going to make a file.

46
00:02:48,750 --> 00:02:50,900
And let's just call it cats.

47
00:02:51,080 --> 00:02:54,160
Yes I've been doing too much dog stuff.

48
00:02:54,210 --> 00:02:55,440
I know I'm going to get complaints.

49
00:02:55,530 --> 00:02:59,840
To do some cats so well to catch serious and then inside of that.

50
00:02:59,910 --> 00:03:00,660
Let's open it up

51
00:03:03,600 --> 00:03:10,100
and before we do anything else I'm going to install mongoose which is a package.

52
00:03:11,270 --> 00:03:18,190
There we go and let's make some room here and the next thing that we want to do is require mongoose

53
00:03:18,190 --> 00:03:28,260
who will do our mongoose equals require mongoose and what we're going to do first is focus on adding

54
00:03:28,560 --> 00:03:37,650
a new cat to the database and then later we're going to retrieve all cats from the database and we'll

55
00:03:37,650 --> 00:03:39,960
do a simple console that log each one

56
00:03:42,810 --> 00:03:43,790
just like that.

57
00:03:44,100 --> 00:03:47,950
OK so we're going to start by figuring out how we add a new cat to the database.

58
00:03:48,090 --> 00:03:52,630
And before we can even do that there's a little bit more set up we need to do with Mongoose which is

59
00:03:52,690 --> 00:03:54,780
we need to connect to a database.

60
00:03:54,990 --> 00:03:58,440
So remember how we have this running in the background.

61
00:03:58,440 --> 00:04:00,390
This is our Mongo deman.

62
00:04:00,570 --> 00:04:01,590
We need to keep that running.

63
00:04:01,620 --> 00:04:07,350
So if you turned it off for some reason make sure you turn it back on with that command Man-God and

64
00:04:07,350 --> 00:04:12,180
then we're going to tell mongoose to connect to this server that we have running and that looks like

65
00:04:12,180 --> 00:04:23,820
this mongoose does connect and then a URL that needs to look like this Mongo D-B colon slash slash localhost

66
00:04:24,990 --> 00:04:25,620
slash.

67
00:04:25,770 --> 00:04:27,740
And then whatever we want to come next.

68
00:04:27,780 --> 00:04:29,490
This is the name for our database.

69
00:04:29,700 --> 00:04:37,380
And just like before if we don't have the database created let's say call it cat app I don't have a

70
00:04:37,380 --> 00:04:38,870
cat app database yet.

71
00:04:39,240 --> 00:04:45,450
And if I run this it will connect and try and find cat app it won't find it and it will make cat app

72
00:04:45,450 --> 00:04:46,650
for me.

73
00:04:46,650 --> 00:04:51,210
But if there is one and it does find it it will use the pre-existing cat app.

74
00:04:51,390 --> 00:04:52,660
So I'll just go with cat app.

75
00:04:52,800 --> 00:04:54,000
That's fine by me.

76
00:04:54,600 --> 00:05:00,660
And that will now connect to our database and we can test that out right just running our file node

77
00:05:00,780 --> 00:05:01,550
cat cats.

78
00:05:01,680 --> 00:05:03,210
Yes.

79
00:05:03,210 --> 00:05:05,720
We don't get an error which means that everything works fine.

80
00:05:05,760 --> 00:05:07,420
So we can control C out of that.

81
00:05:07,680 --> 00:05:11,180
And now what we want to do is focus on adding a cat to the database.

82
00:05:11,430 --> 00:05:15,930
And before we can do that we actually need to define what a cat looks like.

83
00:05:16,020 --> 00:05:20,160
So I'm going to do that here and you're going to see some new syntax quite a bit.

84
00:05:20,220 --> 00:05:24,540
A bunch of new methods that come with Mongoose that you've never seen before and I promise I'll explain

85
00:05:24,540 --> 00:05:27,370
them all but I'm going to start by just typing it out.

86
00:05:27,390 --> 00:05:37,980
So we define our cat schema equals new mongoose dot schema with a capital loss which we pass an object

87
00:05:37,980 --> 00:05:48,770
into and then inside the object we're going to say that a cat has a name that's a string an age that's

88
00:05:48,770 --> 00:05:56,130
a number and we can do Breede although I'm realizing that I actually don't know many cat breeds so I'm

89
00:05:56,130 --> 00:06:00,360
going to change this to temper meant which is a really tricky word to spell.

90
00:06:00,390 --> 00:06:01,220
I always forget about this.

91
00:06:01,240 --> 00:06:03,140
A I think he got it right.

92
00:06:03,150 --> 00:06:06,150
So Will do temperament and that will be a string as well.

93
00:06:06,560 --> 00:06:06,930
OK.

94
00:06:06,930 --> 00:06:07,930
And we'll save.

95
00:06:08,280 --> 00:06:10,680
And this doesn't actually do anything to our database.

96
00:06:10,680 --> 00:06:13,110
It just tells mongoose or javascript.

97
00:06:13,230 --> 00:06:18,750
It tells the javascript side of things that I want to be able to add cats to our database and a cat

98
00:06:18,750 --> 00:06:23,250
should be defined as this you might be asking yourself isn't this.

99
00:06:23,250 --> 00:06:23,900
No.

100
00:06:23,900 --> 00:06:25,200
Q Well are non-relational.

101
00:06:25,200 --> 00:06:28,190
Doesn't that mean that I don't have to define a table.

102
00:06:28,530 --> 00:06:29,600
Yes that's right.

103
00:06:29,610 --> 00:06:31,210
This is not defining a table.

104
00:06:31,350 --> 00:06:36,060
This is defining a pattern for our data but it doesn't mean that we're forbidden from adding new stuff

105
00:06:36,180 --> 00:06:38,310
or leaving certain things off.

106
00:06:38,310 --> 00:06:42,930
It's just a nice way of providing structure because we do need some sort of predictable structure in

107
00:06:42,930 --> 00:06:46,440
order to write code that can handle these cats.

108
00:06:46,650 --> 00:06:50,590
Let's say that we want a template to print out name age and temperament.

109
00:06:50,670 --> 00:06:54,050
We need to make sure that every cat has the name age and temperament.

110
00:06:54,270 --> 00:06:57,540
And if it doesn't then we need to be able to anticipate that.

111
00:06:57,570 --> 00:06:59,690
So that's how we define the schema.

112
00:06:59,710 --> 00:07:01,630
But we solve one more thing to do.

113
00:07:01,740 --> 00:07:05,910
And in my experience this next line is a little bit confusing.

114
00:07:06,390 --> 00:07:16,680
So when I type it first cat with a capital C equals mongoose Dopp model and then cat again in quotes

115
00:07:16,680 --> 00:07:21,030
this time and then the schema.

116
00:07:21,120 --> 00:07:22,360
And we're going to save.

117
00:07:22,800 --> 00:07:29,190
So what we did here is we took this schema cat schema which is just a pattern that says every cat has

118
00:07:29,190 --> 00:07:36,570
a name age and temperament and we compiled it into a model and we save it to a variable cat and now

119
00:07:36,570 --> 00:07:43,860
we can use that cat variable with a capital C to make new cats to find cats to remove cats to update

120
00:07:43,860 --> 00:07:44,350
cats.

121
00:07:44,610 --> 00:07:52,890
We'll do everything off of cat so we'll have things like Cat find or cat dot remove or cat dot create

122
00:07:52,890 --> 00:07:53,370
.

123
00:07:53,370 --> 00:07:59,490
So we've created this cat object which is really a pattern for a cat that now has a bunch of different

124
00:07:59,490 --> 00:08:00,830
pieces on it as well.

125
00:08:00,960 --> 00:08:05,370
So this is just the sheer pattern that says a cat has a name age and temperament.

126
00:08:05,610 --> 00:08:11,370
And when we save it to a variable after compiling it into a model it's not just this pattern now but

127
00:08:11,370 --> 00:08:14,060
it actually has all the methods on it that we want.

128
00:08:14,190 --> 00:08:19,170
So it takes that pattern and it builds this complex model that has all of the methods we need to use

129
00:08:19,200 --> 00:08:19,510
.

130
00:08:19,890 --> 00:08:25,410
And it is always a little bit confusing in particular this right here this always is supposed to be

131
00:08:25,410 --> 00:08:28,910
the singular version of the collection name.

132
00:08:28,950 --> 00:08:35,370
So if we give it cat which is what we did here it's going to make a collection called cats and it is

133
00:08:35,370 --> 00:08:37,800
pretty smart as far as how it pluralize is things.

134
00:08:37,800 --> 00:08:40,030
There's a nice little library that does it.

135
00:08:40,140 --> 00:08:44,790
It can pluralized something like person into people without problems at all.

136
00:08:44,790 --> 00:08:51,240
So again this is the name of our singular version of our model which is cat and it will automatically

137
00:08:51,240 --> 00:08:56,690
take that and make a new collection in our database that would look like D-B dumb cats.

138
00:08:56,700 --> 00:09:01,060
Ok so now we have everything we need to do in order to add a cat in.

139
00:09:01,080 --> 00:09:02,740
Now we just need to write the code.
