In this video.
Let's see how we can perform all the CRUD operations that is create, read, update and delete using
Mongoose.
Well, before we can do that, we need to perform two things.
First step is we have connected to MongoDB.
The next step is we need to create what is called schema and then after that we need to create what
is called modal.
These are the two things that you are going to work with any time you want to use Mongoose.
So what is schema?
So let's have a target here.
What we want to do here is that we want to create a database for school and then for school we are going
to create student or courses.
So in that way we need to create a blueprint.
How we can create instances, for example, instances of courses or students and save it into database.
So back to the documentation, you can see that after it has been connected, then we create a schema
to get what is called model and here we can use MongoDB method to get it done.
So let's click on read documentation here and let's see the steps.
Step number one is we need to create what is called schema.
So let's click on defining schema, which is down here, which is this defining schema.
So everything in Mongoose start with a schema and a schema maps to a MongoDB collection and defines
the shape of the document within that collection.
So the next step is the extruded schema for Mongoose and the define the blueprint down here.
So let's do the same thing inside Visual Studio code.
So how can we create schema for students?
So here we go.
We make use of student, then we add what is called schema to it and it's equal to new.
Then we bring in Mongoose Dart schema as a function, and for this function we are going to pass in
configuration.
We're going to be the blueprints of how we can create students.
So in here we pass in object, and inside here we need to specify the fields of the students.
So in what case, let's say that for student, we need the name of the student, the city of the students,
and let's say the course of the student, we need these three fields and this is what we want.
So how can we specify it here?
So inside the schema we need to provide the fields.
The first one is name and name.
You provide the kind of data types and in Mongoose we have couple of data types.
So for the meantime, let me show you the first one is String and for Mongoose, all the data type start
with the uppercase.
For JavaScript, we start with word lua cases.
So for mongoose, always uppercase if it's a number is going to be a number Boolean, Uppercase, Boolean
and many more.
And we know there are types so mongoose even extend the ties of the type that we can use.
So if you go to the official documentation, then you click on schema type.
Here, you're going to see all these schema types that we can use and here are examples of them we can
use.
String Number Data Buffer Boolean MCs, object ID Array, Decimal Map Schema.
These are the common ones that we use string number dates, boolean arrays and sometimes we use map.
Yeah.
So these are the common ones that we are going to use a lot and I believe we are familiar with the other
type when it comes to JavaScript.
So the same thing being applied inside Mongoose.
So here we go.
So the first one we want to accept string of a name.
And what about the city two, we also want to accept a string also.
All right.
The next one going to be the courses is going to be a ray.
So you provide what is called array as that.
So let's say courses and let's say that is a person married, let's say is married, let's add more
fuel to it and we are going to use what is called boolean.
And we can also provide the age of the students, which is a number.
So you can see this is what.
Destruction.
So for this one, we have well-structured what document that we want to create as compared to normal
MongoDB.
There is no structure.
We can just add anything we want and it will be saved into it.
So with this one, if you are trying to save the command of student and the user provide a field which
is not part of this one is not going to be saved, how awesome it is.
So after that, we need to create what is called model.
So let's get into that in the next video.