This course will be retired on August 10, 2021. We recommend "REST APIs with Express" for up-to-date content.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Mongoose's Schemas let us define default values very easily.
Let's say we wanted to create the default
animal to be saved if we don't supply
0:00
any data to the model.
0:05
Mongooses schema lets us as defined
default values very easily.
0:08
Let's change the schema to give
us a goldfish animal as default.
0:13
Instead of just specifying a type,
0:24
we can give each property a type and
a default value.
0:26
Goldfish are small,
0:48
golden and really light in
0:54
comparison to elephants,
0:59
and named Angela?
1:04
Anyway, now we can create
a generic animal with our model by
1:12
passing an empty object into it.
1:17
And when I say a generic animal,
I mean a goldfish.
1:33
If we were just to call
save on the animal,
1:44
under elephant.save,
we could run into problems.
1:48
The save method runs asynchronously.
1:56
It's impossible to know which
save will finish first.
1:59
If the elephant saves finish first,
it will close the database connection.
2:04
The generic animal won't be saved.
2:11
Instead, we'll place the goldfish.save
inside the other's callback and
2:16
put the db.close inside
the goldfish's callback
2:22
to ensure the correct sequence of events.
2:26
Remember we already have
an elephant in the database.
2:59
Running this file again
will create another one.
3:03
Let's empty the animals
collection before we save it.
3:06
The model can do this for us.
3:11
Let's ask the model to empty our animals
collection before we save anything.
3:13
We can do this by calling the remove
method on the animal model and
3:25
passing in an empty object.
3:29
We could specify which documents we
want to remove by passing in a query
3:40
object here.
3:45
But if we leave all of the properties
blank, it will remove all documents.
3:47
A query object would match
properties from itself
3:52
against properties of
documents in the database.
3:55
Any matching documents would be removed.
3:59
We want all the save actions to
take place after the removal, so
4:11
we'll route this code in removes callback.
4:15
Now when we run this script We'll start
with an empty collection every time.
4:52
Let's run this file in the terminal and
then check the database.
5:00
There's the default animal,
a goldfish, and an elephant.
5:19
Let's save one more animal, a whale,
5:37
to show that these defaults
can be overwritten.
5:39
Let's create a variable called whale and
call the animal constructor.
5:44
The type is whale.
5:59
The size is big.
6:06
The mass is 195,500 kilograms.
6:14
Wow.
6:23
And its name is Fig.
6:28
Notice we're not supplying the color here.
6:36
The whale should pick up
the color from the defaults.
6:40
Let's save it after we save
our other two animals.
6:47
Before we run this, let's also see how
we can read animals out of the database.
7:11
Let's ask the model for
all big animals, and
7:17
then write out their names to the console.
7:20
The models find method takes in
an object as its first parameter.
7:30
This object should contain keys and
values you want to match to your results.
7:38
The second parameter is a callback
function that passes in an error object
7:50
and the results from your query.
7:55
Let's print out each one of
the results in the console now.
8:18
console.log(animal.name +) "
8:22
the " animal.color,
8:31
+" " + animal type.
8:37
Just a note here.
9:12
You see this pattern we're building
up with all these callbacks?
9:14
This is a common pattern in Node, which
you may have seen or experienced before.
9:18
Manage an asynchronous calls by
nesting them inside a callback
9:23
after another callback.
9:26
This is known as the pyramid of doom.
9:28
But we're just playing around
with Mongoose to just get a feel
9:31
of how it works.
9:34
Our code in the rest API we're going to
build won't suffer from this as much.
9:35
Special objects called promises can
help us manage asynchronous code and
9:40
make it easier for
us to read and work with.
9:44
Treehouse content that covers
promises is in the teachers notes.
9:47
However, we're not using
promises in this course.
9:51
Also, we'll clean up a lot of
this code in the next video.
9:54
In the terminal, run this file again.
10:01
The data was successfully submitted and
10:07
that the models were found that
are big animals that we asked for.
10:09
There we have the golden whale, Fig.
10:14
Fig is golden because the default
values were specified in the schema.
10:17
So far, we've performed three of the four
basic database operations with Mongoose,
10:22
namely reading, writing, and deleting.
10:27
Updating is a similar process.
10:31
We'll cover that later.
10:33
Now let's look at some more advanced
things you can do with Mongoose.
10:35
You need to sign up for Treehouse in order to download course files.
Sign up