Welcome back.
In this video, let's see how we can find our students.
So let's go to the Mongoose website and let's search for find method.
So again, under API and queries, click on that and we're going to see all the queries or the API.
If you scroll down, I'm going to see what is called find.
It's my round, I'm able to see it and we have a query, dot prototype, dot find.
So the query is going to be the model.
And then on that we have what is called prototype because with this one we can have access of all the
methods on the instances of the objects.
That is our model.
So if I click on that, let's read more about the Find API and we have what is called the format here
and the first parameter we take is called filter, meaning we can pass in optional argument and what
is filter?
And then I'm going to be an object with key value per.
And next is we pass in callback function, but we are using promise and after that it retains what is
called query, which is this.
This represents the instance of the document that we are trying to find.
And down here it has a couple of example using the async await and using the callback method.
So let's see how I'm going to use it.
So back to our code here.
Let me comment this one.
Otherwise it's going to create duplicate students.
So here are going to be find or student.
So here you go on this student model, we have a method called find and that is it.
And remember because it's returns a promise, I need to handle it using promise.
And here I have access to the individual student as students.
When I use error function and console.log, they are going to be students, many students.
So let me add here as student, okay, so here, if something goes wrong, let's catch it here and I
have access to the error here and then cancel the error as that.
So moment of truth, let's save it.
And back to the terminal.
Let's run the file again and let's see.
I'm going to see all the students.
And you see that we have an array, which means that it retains all the record.
What is this?
We have John and Emmanuel.
So again, with a find we can pass in additional arguments as has been stated.
So let's say that we want to find all student whose age is equal to 20 years and we can do that.
So let's go ahead and create one student with different age.
Now we going to see our query that we are going to run.
So let me bring back the create and comment the find method here.
So let me change the age, say 30 here and the name of the student.
Let me call this one as age one and the person is from USA and with different courses, let's say three
as that and let's see only one, only one cause here and I think it's cool.
So back to the terminal.
Let me clear it here and let me run it and let's see what we are going to get.
All right.
Now, this student has been created, so let me go ahead and fetch all the students.
Now, you have three students with different ages.
So now here we go.
How can you find our student whose age is equal to 20 years?
So find a student who's.
Age is equal to 20 years.
So here we go.
You bring in the student and then find an inside aide.
We pass in additional parameters called queries.
So here you're going to say that if the age.
Is equal to 20 years.
That is it may.
Then we need to handle the promise using this.
So I have access to the actual student and then I can go ahead and console.log.
The student here.
And if something goes wrong, why not?
We will catch it here as error.
I start as the.
Error.
All right, so let's save it.
Thing is.
STUDENT Yeah.
Yeah.
You're going to be error.
Okay, so now let's comment this one and let's see if you're going to get the actual student.
Let's see.
Run it.
And now let's see what we have.
And you can see that we have two student whose age are 20 and 20 years, and we can write this same
logic with different syntax.
So let me show you how we did when I was in Normal or MongoDB.
We see that for MongoDB we also use the fine method, but instead we need to convert the cursor to array.
But with Mongoose we don't do that.
By default it convert the data into documents.
That's why Mongoose makes our life easier when working with MongoDB.
So that is how we can make use of the find.
We can find a user with different properties.
Then this video.
Let's see how we can find a single user.