1
00:00:05,990 --> 00:00:07,100
Welcome back.

2
00:00:07,130 --> 00:00:11,060
In this video, let's see how we can find our students.

3
00:00:11,240 --> 00:00:16,329
So let's go to the Mongoose website and let's search for find method.

4
00:00:16,340 --> 00:00:25,090
So again, under API and queries, click on that and we're going to see all the queries or the API.

5
00:00:25,100 --> 00:00:28,340
If you scroll down, I'm going to see what is called find.

6
00:00:28,340 --> 00:00:34,970
It's my round, I'm able to see it and we have a query, dot prototype, dot find.

7
00:00:34,970 --> 00:00:37,910
So the query is going to be the model.

8
00:00:37,910 --> 00:00:45,080
And then on that we have what is called prototype because with this one we can have access of all the

9
00:00:45,080 --> 00:00:47,900
methods on the instances of the objects.

10
00:00:47,930 --> 00:00:49,040
That is our model.

11
00:00:49,130 --> 00:00:58,460
So if I click on that, let's read more about the Find API and we have what is called the format here

12
00:00:58,580 --> 00:01:05,420
and the first parameter we take is called filter, meaning we can pass in optional argument and what

13
00:01:05,420 --> 00:01:06,230
is filter?

14
00:01:06,230 --> 00:01:09,410
And then I'm going to be an object with key value per.

15
00:01:09,560 --> 00:01:16,580
And next is we pass in callback function, but we are using promise and after that it retains what is

16
00:01:16,580 --> 00:01:18,590
called query, which is this.

17
00:01:18,590 --> 00:01:23,270
This represents the instance of the document that we are trying to find.

18
00:01:23,390 --> 00:01:30,950
And down here it has a couple of example using the async await and using the callback method.

19
00:01:30,950 --> 00:01:33,080
So let's see how I'm going to use it.

20
00:01:33,080 --> 00:01:35,720
So back to our code here.

21
00:01:36,020 --> 00:01:37,790
Let me comment this one.

22
00:01:38,000 --> 00:01:40,820
Otherwise it's going to create duplicate students.

23
00:01:40,820 --> 00:01:45,770
So here are going to be find or student.

24
00:01:45,770 --> 00:01:53,510
So here you go on this student model, we have a method called find and that is it.

25
00:01:53,510 --> 00:02:00,470
And remember because it's returns a promise, I need to handle it using promise.

26
00:02:00,710 --> 00:02:05,180
And here I have access to the individual student as students.

27
00:02:05,180 --> 00:02:10,850
When I use error function and console.log, they are going to be students, many students.

28
00:02:10,850 --> 00:02:19,280
So let me add here as student, okay, so here, if something goes wrong, let's catch it here and I

29
00:02:19,280 --> 00:02:25,460
have access to the error here and then cancel the error as that.

30
00:02:25,460 --> 00:02:29,030
So moment of truth, let's save it.

31
00:02:29,030 --> 00:02:31,250
And back to the terminal.

32
00:02:33,050 --> 00:02:36,800
Let's run the file again and let's see.

33
00:02:36,830 --> 00:02:39,410
I'm going to see all the students.

34
00:02:39,620 --> 00:02:45,590
And you see that we have an array, which means that it retains all the record.

35
00:02:45,590 --> 00:02:47,180
What is this?

36
00:02:47,180 --> 00:02:49,880
We have John and Emmanuel.

37
00:02:50,060 --> 00:02:56,660
So again, with a find we can pass in additional arguments as has been stated.

38
00:02:56,870 --> 00:03:04,490
So let's say that we want to find all student whose age is equal to 20 years and we can do that.

39
00:03:04,490 --> 00:03:08,510
So let's go ahead and create one student with different age.

40
00:03:08,510 --> 00:03:13,040
Now we going to see our query that we are going to run.

41
00:03:13,040 --> 00:03:18,050
So let me bring back the create and comment the find method here.

42
00:03:18,230 --> 00:03:23,540
So let me change the age, say 30 here and the name of the student.

43
00:03:23,570 --> 00:03:33,020
Let me call this one as age one and the person is from USA and with different courses, let's say three

44
00:03:33,410 --> 00:03:39,290
as that and let's see only one, only one cause here and I think it's cool.

45
00:03:39,290 --> 00:03:40,810
So back to the terminal.

46
00:03:41,210 --> 00:03:47,240
Let me clear it here and let me run it and let's see what we are going to get.

47
00:03:47,930 --> 00:03:48,530
All right.

48
00:03:48,530 --> 00:03:53,210
Now, this student has been created, so let me go ahead and fetch all the students.

49
00:03:53,210 --> 00:03:57,020
Now, you have three students with different ages.

50
00:03:57,020 --> 00:03:58,940
So now here we go.

51
00:03:59,390 --> 00:04:04,460
How can you find our student whose age is equal to 20 years?

52
00:04:04,460 --> 00:04:09,560
So find a student who's.

53
00:04:10,830 --> 00:04:13,590
Age is equal to 20 years.

54
00:04:13,590 --> 00:04:14,670
So here we go.

55
00:04:14,760 --> 00:04:20,250
You bring in the student and then find an inside aide.

56
00:04:20,279 --> 00:04:23,790
We pass in additional parameters called queries.

57
00:04:24,060 --> 00:04:27,660
So here you're going to say that if the age.

58
00:04:28,780 --> 00:04:31,270
Is equal to 20 years.

59
00:04:31,810 --> 00:04:32,770
That is it may.

60
00:04:33,100 --> 00:04:36,520
Then we need to handle the promise using this.

61
00:04:36,520 --> 00:04:43,330
So I have access to the actual student and then I can go ahead and console.log.

62
00:04:43,330 --> 00:04:44,560
The student here.

63
00:04:46,480 --> 00:04:49,300
And if something goes wrong, why not?

64
00:04:49,330 --> 00:04:51,670
We will catch it here as error.

65
00:04:54,250 --> 00:04:56,020
I start as the.

66
00:04:58,260 --> 00:04:58,890
Error.

67
00:05:00,510 --> 00:05:01,950
All right, so let's save it.

68
00:05:01,980 --> 00:05:03,030
Thing is.

69
00:05:03,100 --> 00:05:04,830
STUDENT Yeah.

70
00:05:05,850 --> 00:05:06,390
Yeah.

71
00:05:07,140 --> 00:05:09,210
You're going to be error.

72
00:05:09,260 --> 00:05:16,140
Okay, so now let's comment this one and let's see if you're going to get the actual student.

73
00:05:17,760 --> 00:05:18,510
Let's see.

74
00:05:18,510 --> 00:05:19,320
Run it.

75
00:05:19,320 --> 00:05:21,990
And now let's see what we have.

76
00:05:22,110 --> 00:05:29,940
And you can see that we have two student whose age are 20 and 20 years, and we can write this same

77
00:05:29,940 --> 00:05:31,710
logic with different syntax.

78
00:05:31,710 --> 00:05:36,480
So let me show you how we did when I was in Normal or MongoDB.

79
00:05:36,780 --> 00:05:44,850
We see that for MongoDB we also use the fine method, but instead we need to convert the cursor to array.

80
00:05:44,910 --> 00:05:47,310
But with Mongoose we don't do that.

81
00:05:47,310 --> 00:05:51,270
By default it convert the data into documents.

82
00:05:51,510 --> 00:05:55,710
That's why Mongoose makes our life easier when working with MongoDB.

83
00:05:55,830 --> 00:05:58,740
So that is how we can make use of the find.

84
00:05:58,740 --> 00:06:01,680
We can find a user with different properties.

85
00:06:01,770 --> 00:06:02,760
Then this video.

86
00:06:02,760 --> 00:06:05,970
Let's see how we can find a single user.

