In the last section we did a quick review on what we're trying to accomplish right now at the very end
we had said that we need to create a new model class using mongoose which will allow us to create a
new collection of records inside of our Mongo database.
So right now we want to make this model class which represents a huge collection of records.
Now to do so I'm going to flip back over to my code editor.
We need to find some suitable location to create this model class.
Now I don't think it really belongs inside of our index yes file because this is really associated with
kind of booting up or starting up our application.
So instead I think that I'm going to make a new directory inside of my project.
So a new folder I'm going to call it models.
So this is going to contain all the different models or model classes that we create using mongoose
inside of this file or Symeon sided folder.
We'll make a new file.
I'm going to call it user dot jaywalks.
OK.
So inside of here we will create our mongoose model class to do so at the very top we will require in
the mongoose library like so and then we're going to pull one property off of this mongoose object we'll
say Konst schema equals mongoose dot scheme.
Now you'll notice that the value or the property name schema right here is identical to the variable
name that we are assigning it to.
So we can use a little bit of yes 20:15 destructuring just to make sure we're on the same page here.
Just one time we're going to talk a little bit about destructuring very quickly.
So the code that we wrote right here can be condensed down to just curly braces schema equals mongoose
like the so line to right here and line three 100 percent completely equal.
This line right here says the mongoose object has a property called schema.
Take that property and assign it to a new variable called schema.
That's what these curly braces indicate.
We're going to be using destructuring like this throughout this course.
A tremendous amount.
And at every point in the future I will do a very brief mention of the fact that we were using the structuring
but I will probably not go through the entire process of saying oh these two things are equivalent again.
OK.
Oops.
OK.
So we've got the mongoose library and we've got this schema thing right here.
Now what in the world are we even use in schema for right like what does this for.
Well let's make sure it's really clear.
Just a moment ago when we started talking about Mongo in general we had said that a collection has many
records right.
We spoke about that and we had also said that every record inside of a collection can have its own unique
set of properties.
So for example this first record right here has a height property.
But the second one does not.
This record has an age property but no other record does.
So we had said that with Mago we can have as many random arbitrary properties as you could possibly
imagine on any different record how ever.
However when we make use of mongoose it kind of curtails or kind of removes that ability.
Mongoose wants to know all of the different properties that our records will have inside of our database.
And it requires us to define all those ahead of time with this schema object.
So when we make use of mongoose we kind of lose out on the ability to have just a bunch of random different
properties on each individual record because mongoose wants to know ahead of time all the different
properties that we might have.
So right underneath our two requires statements or one require statement I should say we are going to
use this schema object to create a schema for this new collection.
The schema will describe what every individual property personally every individual record is going
to look like.
So I'm going to say Konst user schema equals new schema.
And then we're going to pass it an object.
This object right here is going to describe all the different properties we have.
So right now I can really only think of like maybe one very distinct property that I know we're going
to have and that is our users Google ID or the ID out of their profile.
So I'm going to say that every user inside of our users collection will have a property called google
id like so.
And that is going to be of type string.
Now two things I want to point out in great detail.
Notice that I have a lower case D here.
You can use a capital D.
You just have to be consistent everywhere throughout your application.
I'm going to use the lower case like so now the second thing is the value that we assign to this string
that tells our schema that any time there is a value on this google id property it will always be a
string and it should treat it like a string.
If this was instead a number we would say this is a number.
In our case the Google ID is a string.
Now if you remember looking at the Google ID inside of our profile when we cancel logged it much earlier.
Let's see.
I don't have it up anymore but when we have previously looked at our Google ID you might have noticed
that it was only a sequence of numbers.
However those numbers were wrapped inside of a string.
So we're just going to call the thing a string and call it a day OK.
So at this point we have created a schema that describes what a user will look like and we're only going
to care about the google id property for now.
Now if you create an actual model class and tell mongoose that it needs to be aware that this new collection
needs to be created we are going to add one line of code at the bottom which will say mongoose model
the very first argument will be the name of the collection which is going to be users because it's our
users collection it's a collection of all the different users inside of our application.
And then the second argument will be that user schema that we just created so will say user schema like
so.
OK that's pretty much it.
So by using this mongoose model command we are telling mongoose that we want to create a new collection
called users.
Now when Mungo's boots up if this collection already exists it's not going to delete it and remake it.
It's just going to say oh OK it looks like there's already a collection of users in the database and
I've got some scheme for something called the users.
So these two things might match up smuggest will not overwrite existing collections.
It will only create it if it does not already exist.
Now one thing I want to say one last thing about scheme us we can freely add in and remove properties
as we see fit.
So at some point we might decide that we want to get our users name like their first name.
We can easily add in an additional property without any mass whatsoever so freely add or subtract properties
here as you please.
OK.
One last thing remember that whenever we create a javascript file inside of our project just creating
it does not mean that the file actually gets executed.
So we need to make sure that we actually require this file to somewhere inside of our project structure.
So right now I'm going to change on over to my index file and I will require in the models slash user
file that we just created.
So that will ensure that whenever our application first boot up all of this configuration will load
and longus will be informed that it needs to be responsible for creating a collection of users.
OK.
So that's pretty much it.
We have now created our first model class.
This right here loading this model into mongoose that creates our model class.
So let's now continue in the next section and we will use that model class to create a new record inside
of our list of all of our different users.
Any time a user signs into our application for the very first time so I'll see you in just a second.