Welcome back.
This is the time that is we are going to create record and save into MongoDB using Mongoose.
So let me have beautiful comment here and says that great students.
All right, so now we need to learn mongoose method.
So, like I said, we can use any MongoDB method, for example, insect fine and many more.
And we can also use mongoose method.
So back to Mongoose website and now click on the and the API is click on query.
And here we are going to see all the query method we can use.
You can see a bunch of them.
We see that is our sun.
We can perform more operations using this mongoose query method.
So you can see that if we scroll down for Mongoose, we have a couple of methods that's going to help
us to insect data, and we have one called Create.
So let's go down and let's locate what is called create.
Sometimes the find is difficult.
We can make a search for it.
So let's see if I'm going to see create here.
Let's have some patient and let's see create this delete count.
Okay.
Is nowhere to be found here.
Maybe I can see.
So let's search for create here.
If we search for create now I'm going to see the method for creating.
And you can see that we have AP High and then the model name called Create.
So when I click on that, I'm going to see the format for creating and here we go model which represents
the model that we have created here, which is this one.
And on that we are going to use method for mongoose called create.
And that is it.
So let's look at the parameters it takes.
First is it takes in the object and then document to insect so we can insert array that is to create
money or a single object.
And we can pass in additional option as that and then callback function and always it retains promise.
So it means that we can use callback or primes or async await to get it done.
So looking at the example here, they use a weight in front of the model and they use the method to
create the parse in the details they want to create.
And that is all and that is all.
So let's see how we can achieve that inside our code.
So here we go.
So first is we bring in this student model.
And on that we see I have create here and we can use insets from MongoDB.
So let's use create as that.
And for the create you pass in the details, we want to create as an object with key value per.
And remember, we are going to follow this format.
So the first one is name.
So I will provide name as John.
And then after that, the city.
Going to say from Kumasi.
And after that, the courses the person is going to take is going to be array of courses.
So let's say computer science and let's say.
English.
And then let's say administration and let's maintain this one for now.
After that, we need to provide the two properties is married and the age.
So here we can provide is married.
So let's provide true the person is married.
And then the age of the students, let's say 20 years.
So now we are done with it.
So remember model always return a promise, meaning we need to await for success and failure.
So we are going to use promise here, pass in the actual response student and I can cancel, log it
and see whether it has been created or not as the student.
And if something happens, we're going to catch it as an error pass in here and I'm going to get it
as an error here console.log.
The error as that.
So now moment of truth.
So let's go back and then restart our file, clear the terminal and let's run the file again.
And let's see.
A and you could see that it has been created for me successfully with name John city command C courses
as that is merit age and I have two pairs for underscore ID and that's that.
V So don't worry about this one.
Let's focus on the ID.
So let's go ahead and make sure that we see this data inside our MongoDB and we are going to use the
compass to visualize it.
So click on this icon to refresh.
And next step is we need to locate our database.
But remember, for Mongoose, when you are connecting or creating a record, we didn't specify the database
name, so by default it will give us a test database.
So if I click on that, we see that now I have the student record when I open it and indeed I'm going
to see the student here.
So how can we customize this one?
So back to the connection string here where we connect, you can see that we have this you are error.
And then first last question mark and then right, so here we go.
So here we are going to specify the name of our database.
So here we call it at school.
That is it.
So let's save this one.
And then next step is let's go ahead and run the file again.
We are going to create the same student and it has been created.
We have duplicate uses, but don't worry.
So let's get back to the compass and again, let's refresh it.
And now I can see that instead of test we have database called school.
So now let's remove the test one and let me type the name of my database called test and drop it and
let's focus on the school.
And indeed, under school we have one collection called Student and here we go.
All right.
That is all some guys and you can see that number of record here or the command, which is one.
And we can go ahead and create more students by passing in different names, let's say Emmanuel and
from Akra.
And let's see.
I like these courses and I'm not married.
All right.
So with this one, let's go ahead and then restart the file.
The terminal.
And let me run the file again.
Let me see.
Connected.
And then record has happened.
And here we go.
Let me refresh my compass here.
I'm going to see two students and indeed we have eight.
And as you see the number of documents.
So we can also create many students using create many or insect money so we can read more about the
APIs which help us to get this one done.
The next video.
Let's see how we can fetch or record.