OK welcome back.
So far we've seen how to interact with Mongo TV using the Mongo console which as I mentioned is nice
for teaching things and for testing things out and debugging but it's not where we'll actually write
the bulk of our code that interacts with the database where we'll actually be doing that is instead
of our expressed code.
So they pull up an example let's go to Yelp camp and just take a look at her app.
Yes.
Here's an example of where we would use mangu.
So for instance here when we're adding a new campground to our array rather than adding campgrounds
to an array you would have a database a mongo database and a collection called campgrounds.
And this is actually coming up very soon in this series.
And then when you submit a post request to slash campground rather than pushing into an array we're
going to insert into the Mongar DP collection likewise appear on the get slashed campgrounds rather
than just rendering campgrounds with the array campgrounds that's defined here.
We're actually going to do a find a D-B campground find and then take the results of that and send that
to the campground template.
OK.
So we'll get there.
So that's what we're focusing on this lesson.
And in order to do that we're going to learn about a tool called mongoose.
So I have three main goals on it tell you what mongooses on explain why we're using it.
And I also want to show you how to use it instead of a javascript file.
Let's start by talking about what Mongoose is.
So I have the official home page open.
It says that Mongoose is an elegant Mongo DB object modeling for no genius.
OK.
Let's read a little bit more.
Mongoose provides a straightforward schema based solution to model your application data.
It includes built in typecasting validation queery building business logic coax and more out of the
box.
So I think this would make more sense to a seasoned developer.
But what it really means is that Mongoose is a tool.
It's a package that we're going to download with NPM that helps us interact with Mongo D-B instead of
our javascript files.
It is possible to do it without mongoose.
There were other tools like this out there but it just makes it easier for us to interact with the database
just like Jay Querrey makes it easier for us to interact with the Dom but we don't have to have a query
.
Mongoose makes it easier and cleaner for us to interact with the monkhood DB database but it's not necessary
.
So let's head back to cloud 9 and I'm going to go ahead and make a single javascript file.
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
Yes and when I run the file we want it to add something to our database using mongoose.
So I'll get started.
I'm inside of a directory that I made called databases and I'm going to make a file.
And let's just call it cats.
Yes I've been doing too much dog stuff.
I know I'm going to get complaints.
To do some cats so well to catch serious and then inside of that.
Let's open it up
and before we do anything else I'm going to install mongoose which is a package.
There we go and let's make some room here and the next thing that we want to do is require mongoose
who will do our mongoose equals require mongoose and what we're going to do first is focus on adding
a new cat to the database and then later we're going to retrieve all cats from the database and we'll
do a simple console that log each one
just like that.
OK so we're going to start by figuring out how we add a new cat to the database.
And before we can even do that there's a little bit more set up we need to do with Mongoose which is
we need to connect to a database.
So remember how we have this running in the background.
This is our Mongo deman.
We need to keep that running.
So if you turned it off for some reason make sure you turn it back on with that command Man-God and
then we're going to tell mongoose to connect to this server that we have running and that looks like
this mongoose does connect and then a URL that needs to look like this Mongo D-B colon slash slash localhost
slash.
And then whatever we want to come next.
This is the name for our database.
And just like before if we don't have the database created let's say call it cat app I don't have a
cat app database yet.
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
for me.
But if there is one and it does find it it will use the pre-existing cat app.
So I'll just go with cat app.
That's fine by me.
And that will now connect to our database and we can test that out right just running our file node
cat cats.
Yes.
We don't get an error which means that everything works fine.
So we can control C out of that.
And now what we want to do is focus on adding a cat to the database.
And before we can do that we actually need to define what a cat looks like.
So I'm going to do that here and you're going to see some new syntax quite a bit.
A bunch of new methods that come with Mongoose that you've never seen before and I promise I'll explain
them all but I'm going to start by just typing it out.
So we define our cat schema equals new mongoose dot schema with a capital loss which we pass an object
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
a number and we can do Breede although I'm realizing that I actually don't know many cat breeds so I'm
going to change this to temper meant which is a really tricky word to spell.
I always forget about this.
A I think he got it right.
So Will do temperament and that will be a string as well.
OK.
And we'll save.
And this doesn't actually do anything to our database.
It just tells mongoose or javascript.
It tells the javascript side of things that I want to be able to add cats to our database and a cat
should be defined as this you might be asking yourself isn't this.
No.
Q Well are non-relational.
Doesn't that mean that I don't have to define a table.
Yes that's right.
This is not defining a table.
This is defining a pattern for our data but it doesn't mean that we're forbidden from adding new stuff
or leaving certain things off.
It's just a nice way of providing structure because we do need some sort of predictable structure in
order to write code that can handle these cats.
Let's say that we want a template to print out name age and temperament.
We need to make sure that every cat has the name age and temperament.
And if it doesn't then we need to be able to anticipate that.
So that's how we define the schema.
But we solve one more thing to do.
And in my experience this next line is a little bit confusing.
So when I type it first cat with a capital C equals mongoose Dopp model and then cat again in quotes
this time and then the schema.
And we're going to save.
So what we did here is we took this schema cat schema which is just a pattern that says every cat has
a name age and temperament and we compiled it into a model and we save it to a variable cat and now
we can use that cat variable with a capital C to make new cats to find cats to remove cats to update
cats.
We'll do everything off of cat so we'll have things like Cat find or cat dot remove or cat dot create
.
So we've created this cat object which is really a pattern for a cat that now has a bunch of different
pieces on it as well.
So this is just the sheer pattern that says a cat has a name age and temperament.
And when we save it to a variable after compiling it into a model it's not just this pattern now but
it actually has all the methods on it that we want.
So it takes that pattern and it builds this complex model that has all of the methods we need to use
.
And it is always a little bit confusing in particular this right here this always is supposed to be
the singular version of the collection name.
So if we give it cat which is what we did here it's going to make a collection called cats and it is
pretty smart as far as how it pluralize is things.
There's a nice little library that does it.
It can pluralized something like person into people without problems at all.
So again this is the name of our singular version of our model which is cat and it will automatically
take that and make a new collection in our database that would look like D-B dumb cats.
Ok so now we have everything we need to do in order to add a cat in.
Now we just need to write the code.