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 the code that looks like this.
We're going to make a new cat and when we make a new cat We're going to pass in data.
So I'm going to say name and let's go with George and his age.
Let's go with an older cat 11 and temperament is grouchy.
Just going off of my experience with cats.
And then what we'll also do is save this to a variable.
So we'll call it far.
George equals new cat.
Her name is George.
Age 11 temperament is grouchy.
So this doesn't actually added to the database just yet.
There's another step which is George Dodd save.
And once we save it will be added to the database.
However there's a possibility that it won't save let's say that our Mangu process isn't running for
some reason or the Internet is not working on a machine it might potentially not save.
So rather than just saving it blindly what we can do is pass a callback function into save and this
function will be called when the save is done.
Whether it worked or not and it will give us two different things.
The first is any potential error and the second is it must not call it George is the item that's returned
to the item that was saved.
It does call it cat.
And what we can do is check if there's an error.
We'll just do a concert vault log.
Something went wrong just like that.
Otherwise let's do a come to that log.
We just saved a cat to the database and then we're also called Salt log the cat.
All right.
So there's a lot here.
So let's walk through this one more time.
Let's start with just the save.
So what we're doing is trying to save George the cat to the database and repassing in this function
that will be called once this is done.
And this is a pattern that we're going to see all the time in mongoose.
So we'll have things like that find and then you pass in a callback function to that and then we'll
do the same thing for Dot remove and for Dot create in pretty much everything we do with Mongoose will
involve a callback function and that's because this takes time.
Javascript has to go and communicate with this process here and figure out if it went right or went
wrong and it takes time.
So we have to do the callback in order to have code execute after this is done.
OK.
So our callback takes two parameters.
The first one is a potential error.
And this will be empty hopefully but if not then we're going to cancel that log.
Something went wrong.
Otherwise we'll print out.
We just added We just saved your cat to the database and we'll print out the cat.
OK.
A lot of stuff.
So let's see how this goes.
Let's try running this node cat stuff.
Yes.
OK.
And we get we just saved the cat to the database and we got the cat that we printed out with this line
here.
And you can see that we have an I.D. temperaments Graci age 11.
Name is George.
And I just want to highlight here Kat is referring to what came back from the database because we also
have George and we could also conc about LUGG George if we wanted to and we would see that it's slightly
different.
George is not what's coming from a database.
George is what we have in javascript that we're trying to save to the database and that is what's being
sent back from the database.
So let me prove to you that this worked.
We can open up our Mongo con. And then if we do show Digby's we now have cat app so we can use cat out
.
And then what we can do is show collections first and we have cats which was automatically created by
pluralizing this and then let's just do a DVD cat stuff find all and we have our new cat here George
.
OK.
Let's exit out and now let's create one more cat.
So change the name of the cat from George to Mrs. Norris which is the cat from Harry Potter films his
cat and I have no idea how old she is let's just say seven although probably much older than that.
And her temperament will be evil although to be fair we could say that about every cat ever in the history
of the world.
And yes it still says George and that's fine because this doesn't actually impact anything.
It's just the name of the variable on the javascript's side of things.
But remember we're just writing javascript.
But then we're saving it here.
And it takes the data inside and sends that to Mongo and saves it in the database.
But this has nothing to do with it.
That's just how we refer to it instead of our code.
OK so let's try the same thing.
Try and save that cat again.
Run node Julius or cat Stut Julius OK and we get we just save another catch to the database and the
90 temperaments evil age seven name is Mrs. Norris.
OK.
So that's how we can add cats to the database.
The next important feature is retrieving cats from the database and to do that we'll go back here and
down here I'm going to show you a new method.
And actually I'm going to comment this out otherwise every time we run this code we'll get a new cat
in our database and that's fine.
But I only can have one Mrs. Norris at a time.
I don't want to have five or six of them running around in our database.
So I'm going to come it out for now.
So down here what we need to do is use the find method on cat which was our model we defined right here
.
So Cat dot find and we're just going to pass in an empty object.
And then the callback function and this callback function is just like the other one where we take the
error as the first parameter.
If there is potential error and we'll check so we can start there.
If there's an error let's to cancel that log.
Oh no error.
And then we'll also print out the error itself.
Otherwise let's pass in this second parameter which is going to be the result of the dot find which
would be hopefully multiple cats although potentially just one cat but we'll call it cats.
Totally up to us.
Just like this is up to us as well.
Hopefully by now you're recognizing that anytime we have a callback it's completely up to us to decide
what to name these parameters.
So we'll do else cancel that log cats and let's just do that and we'll add another one in here just
to make it easier to see all the cats.
OK.
And save that and let's see if this works.
So we're going to run cat to find and repassing an empty object because we're not looking for any particular
cats.
We just want all of them and then repassing in the callback because remember this takes some time or
it might not work.
There might be an error.
So this code is executed when this is done.
All right.
And I would check if there's an error you printed out.
Otherwise you print out the cats.
Let's now make sure you have everything else commented out except for the important stuff appear and
we'll clear the console and then run our file.
There we go.
So we get our old cat conses that log and we get an array that has two different casts in it.
Great.
So I'm going to show you one more thing we can do to create a cat.
Rather than doing it in two separate steps like we have here where we're making a new cat and then running
.
Save on that cat.
Well we can also do.
I'll come get this out for now.
All of this actually is used a method create which is just like find so it's can create.
And this is basically new and save all at once.
So rather than making it new first and then saving it to making sure it works.
We can just try it right away with Create which will make a new one and save it in a database.
So let's do name.
And I'm running out of ideas here.
Let's just call a cat snow white.
And age is 15 which is a number.
And miraculously we found a cat whose temperament was nice.
Nice is too strong a word.
Let's just go with bland.
There we go.
So we save it and if we run this now it might it might not work.
Well we'll have no way of knowing until we added the callback here.
So again hopefully you're seeing this pattern are going to see it all the time and then we'll do our
if ever and we'll just do this nice and quick.
Cancel that log error.
Else cancelled out log cat.
And remember I call it cat because we are creating a single cat versus down here I called the cats but
it's just a matter of what makes sense for a name.
So in this case cats made more sense here.
Cat makes more sense but we could also call it new cat or newly created Cat or returned cat everyone
.
OK so save.
And let's see if this works.
So now what we've done is first right here we've added this new cat that we created and we got it back
from the database snow age 15 Blanche temperament.
And then we also left this code we didn't commented out.
So we also went and found all of the cats in the database and we got three different cats grouchy evil
and bland and yes those could be their names.
Technically that's their temperament.
Their names are George Mrs. Norris and Snow White.
OK.
So we saw a lot of stuff here.
Let's do a very quick recap and we shrink this down a bit and let's start at the beginning.
So what does mongers Mongoose is what's known as an odium object data mapper.
What it really means is that it's a way for us to write javascript inside of our javascript files like
our app J.S. or express files and that javascript code will interact with our database.
So it's basically a javascript layer on top of Mongo DB and it adds some extra fancy things in that
we haven't really seen yet.
But it has some some extra special methods that makes it easier on us and then we connected the cat
app database was created for us because it didn't exist.
But next time we connect and actually every time we've connected.
Aside from the first time we ran the file we connected to the same cat app that already existed.
And then we defined a schema which is a plan for what a cat looks like a name age and temperament.
Now I didn't mention this but we don't have to have a name age and temperament.
If we left them blank we wouldn't get any errors but we can add that functionality in and we will later
for instance when we have a user signing up on our Web site we don't want them to be able to leave password
blank email blank.
We need both of them to be there.
So there's a way to do that with Mongoose and then we have what I think is the most confusing line conceptually
which is where we take the cat schema.
The plan for a cat.
And we compile it into a model which returns an object that has a bunch of methods so it knows about
our plan name age and temperament.
But it also adds in things like create and find and a whole lot more methods.
So then we save it to cat with a capital C which is conventional to have it be a capital name there
for our model and then we did things like making a new cat and then saving it.
And the key thing is the callback or we have an error.
And then the data coming back.
And then we did create as well or we do cat create same exact idea.
We have a callback and then we have another method down here find.
So I'll be showing you a few more methods as we go.
The point is not really that you memorized the exact syntax of the methods but just conceptually are
clear on how this works.
We install mongoose connect with database schema model and then we interact with the database.
All right so that's it for now and the next video I'm going to show you how you can add mongoose to
an express app and interact with the database inside of an express app