1
1

00:00:01,410  -->  00:00:02,590
<v Jose>Welcome back, in this video,</v>
2

2

00:00:02,590  -->  00:00:05,100
we're going to be implementing the ability to
3

3

00:00:05,100  -->  00:00:08,480
find all of the items that we have saved in our database.
4

4

00:00:08,480  -->  00:00:10,550
To do that, we're going to need to extend
5

5

00:00:10,550  -->  00:00:13,458
the capabilities of our database class.
6

6

00:00:13,458  -->  00:00:15,580
As well as inserting items,
7

7

00:00:15,580  -->  00:00:18,360
we're gonna need to be able to find items.
8

8

00:00:18,360  -->  00:00:22,030
All we have to do is say, staticmethod,
9

9

00:00:22,030  -->  00:00:23,810
that we're gonna define, and it's going to be called
10

10

00:00:23,810  -->  00:00:26,230
find, it's gonna take in a collection, which is a string,
11

11

00:00:26,230  -->  00:00:27,610
and it's gonna take in a query,
12

12

00:00:27,610  -->  00:00:29,190
which is another dictionary.
13

13

00:00:29,190  -->  00:00:31,620
This query is going to match
14

14

00:00:31,620  -->  00:00:33,920
what we're gonna search for in MongoDB.
15

15

00:00:33,920  -->  00:00:35,390
So, for example, let's say
16

16

00:00:35,390  -->  00:00:37,730
we want to search for all the items,
17

17

00:00:37,730  -->  00:00:40,150
we will just pass in an empty query.
18

18

00:00:40,150  -->  00:00:41,930
If we wanna search for items
19

19

00:00:41,930  -->  00:00:44,900
that have a specific ID property,
20

20

00:00:44,900  -->  00:00:48,130
we would pass in the ID property, like this.
21

21

00:00:48,130  -->  00:00:51,754
ID is a specific ID we wanna pass in.
22

22

00:00:51,754  -->  00:00:55,030
All right, so that's the collection and the query.
23

23

00:00:55,030  -->  00:01:00,030
And they're going to return is a pymongo.cursor,
24

24

00:01:00,550  -->  00:01:03,120
and this should be a single arrow there,
25

25

00:01:03,120  -->  00:01:05,847
cursor with a lowercase C there.
26

26

00:01:05,847  -->  00:01:09,480
The cursor in PyMongo is an iterable.
27

27

00:01:09,480  -->  00:01:11,530
It behaves very much like a list,
28

28

00:01:11,530  -->  00:01:14,823
so you can use four loops on it and that sort of stuff.
29

29

00:01:16,120  -->  00:01:17,850
So what this is gonna do is
30

30

00:01:17,850  -->  00:01:20,960
return Database.Database collection
31

31

00:01:20,960  -->  00:01:23,620
and dot find query.
32

32

00:01:23,620  -->  00:01:26,570
Nothing magical there, all we're doing is
33

33

00:01:26,570  -->  00:01:28,870
we're calling the PyMongo library,
34

34

00:01:28,870  -->  00:01:30,780
and we're saying, hey, PyMongo,
35

35

00:01:30,780  -->  00:01:34,240
find elements in this collection with this query.
36

36

00:01:34,240  -->  00:01:36,010
And if we pass in the items collection,
37

37

00:01:36,010  -->  00:01:37,550
and we pass in an empty query,
38

38

00:01:37,550  -->  00:01:39,860
it's gonna get us all the items.
39

39

00:01:39,860  -->  00:01:41,940
Now we can go back to our item model,
40

40

00:01:41,940  -->  00:01:43,660
and we can implement the method
41

41

00:01:43,660  -->  00:01:46,722
that will allow us to get all of the items from the database
42

42

00:01:46,722  -->  00:01:49,843
using that database method we just implemented.
43

43

00:01:50,710  -->  00:01:52,600
I'm going to make it a class method.
44

44

00:01:52,600  -->  00:01:54,350
You'll see why in just a moment.
45

45

00:01:54,350  -->  00:01:55,963
I'm going to call it all.
46

46

00:01:57,410  -->  00:01:59,380
This is because, in the app.py,
47

47

00:01:59,380  -->  00:02:03,270
we want to be able to do item.all,
48

48

00:02:03,270  -->  00:02:08,270
and that should give us a list of item objects.
49

49

00:02:08,910  -->  00:02:13,460
In here, we're going to get all the items from the database.
50

50

00:02:13,460  -->  00:02:17,470
I'm gonna create a variable called items from db,
51

51

00:02:17,470  -->  00:02:20,170
and then, this is gonna be equal to Database.find
52

52

00:02:21,230  -->  00:02:24,430
in the items collection, all of them.
53

53

00:02:24,430  -->  00:02:26,450
And that is the empty query that is needed
54

54

00:02:26,450  -->  00:02:28,840
to find all of the items.
55

55

00:02:28,840  -->  00:02:32,770
So what we have now is a PyMongo cursor.
56

56

00:02:32,770  -->  00:02:37,340
That is very similar to a list of Python dictionaries,
57

57

00:02:37,340  -->  00:02:40,280
where each dictionary has the information present
58

58

00:02:40,280  -->  00:02:41,583
in the items collection.
59

59

00:02:42,500  -->  00:02:46,050
So each of the dictionaries in the database
60

60

00:02:46,050  -->  00:02:49,080
has an underscore ID, a URL,
61

61

00:02:49,080  -->  00:02:51,770
a tag name, and a query.
62

62

00:02:51,770  -->  00:02:54,750
And if you remember, these are exactly the same
63

63

00:02:54,750  -->  00:02:56,877
as we have in our init method,
64

64

00:02:56,877  -->  00:03:01,223
the URL, the tag name, the query, and the underscore ID.
65

65

00:03:02,060  -->  00:03:04,000
So what are we gonna do?
66

66

00:03:04,000  -->  00:03:07,230
Instead of returning items from db,
67

67

00:03:07,230  -->  00:03:10,310
which would give us back, essentially,
68

68

00:03:10,310  -->  00:03:13,090
a list of dictionaries, what we're going to do is
69

69

00:03:13,090  -->  00:03:18,090
we're going to return a cls of item
70

70

00:03:18,570  -->  00:03:21,823
for item in items from db.
71

71

00:03:22,860  -->  00:03:27,440
What this does is it iterates over items from db,
72

72

00:03:27,440  -->  00:03:32,340
so the item is each of the dictionaries in that cursor.
73

73

00:03:32,340  -->  00:03:37,100
For each dictionary, we create a new item object
74

74

00:03:37,100  -->  00:03:39,760
using the cls notation.
75

75

00:03:39,760  -->  00:03:43,670
Cls in a class method is the current class,
76

76

00:03:43,670  -->  00:03:46,323
so this is the same as doing item.
77

77

00:03:47,165  -->  00:03:51,280
And when you pass in the double-asterisk item
78

78

00:03:51,280  -->  00:03:54,110
to the constructor, what it's saying is,
79

79

00:03:54,110  -->  00:03:57,440
pass in four arguments.
80

80

00:03:57,440  -->  00:04:00,090
ID should be equal to the ID property
81

81

00:04:00,090  -->  00:04:01,730
that's coming in from the database,
82

82

00:04:01,730  -->  00:04:03,590
the URL should be equal to URL property
83

83

00:04:03,590  -->  00:04:04,860
that's coming in from the database,
84

84

00:04:04,860  -->  00:04:07,410
and so on for the four of them.
85

85

00:04:07,410  -->  00:04:09,500
So when you do this, really, what you're doing is
86

86

00:04:09,500  -->  00:04:14,500
you're saying underscore ID equal item underscore ID,
87

87

00:04:14,801  -->  00:04:19,801
URL equal item URL, and so on for each of the properties.
88

88

00:04:20,550  -->  00:04:23,662
But because the parameters in the init method
89

89

00:04:23,662  -->  00:04:27,980
have the same name as the keys
90

90

00:04:27,980  -->  00:04:30,400
in the dictionary that we've saved,
91

91

00:04:30,400  -->  00:04:34,740
we can just do double-asterisk item.
92

92

00:04:34,740  -->  00:04:36,460
And what this is gonna give us is now,
93

93

00:04:36,460  -->  00:04:39,580
instead of a list of dictionaries,
94

94

00:04:39,580  -->  00:04:41,523
or rather, a cursor of dictionaries,
95

95

00:04:41,523  -->  00:04:45,340
it's going to give us a list of item objects.
96

96

00:04:45,340  -->  00:04:47,210
So why is that useful?
97

97

00:04:47,210  -->  00:04:51,410
Well, because now that we have a list of item objects,
98

98

00:04:51,410  -->  00:04:55,870
we can load the price of one of them.
99

99

00:04:55,870  -->  00:04:58,710
Now, we have access to this method.
100

100

00:04:58,710  -->  00:05:01,410
If this was a list of dictionaries,
101

101

00:05:01,410  -->  00:05:04,100
obviously, you wouldn't have the method on a dictionary.
102

102

00:05:04,100  -->  00:05:07,060
You would have to create the item on the check first.
103

103

00:05:07,060  -->  00:05:09,240
This method is only available on item objects,
104

104

00:05:09,240  -->  00:05:10,960
and not on dictionaries.
105

105

00:05:10,960  -->  00:05:13,690
So now that we have this, we can run the app,
106

106

00:05:13,690  -->  00:05:18,690
and as you can see, we get first the printed list of items,
107

107

00:05:20,340  -->  00:05:22,630
and this means that I have two items
108

108

00:05:22,630  -->  00:05:24,090
at the moment in my database.
109

109

00:05:24,090  -->  00:05:25,930
They are same, they have the same URL,
110

110

00:05:25,930  -->  00:05:28,590
that's because I've saved it twice from the app twice.
111

111

00:05:28,590  -->  00:05:29,707
And then, we're gonna get the first one,
112

112

00:05:29,707  -->  00:05:31,760
and we're gonna load the price of it,
113

113

00:05:31,760  -->  00:05:34,113
and that's 1469.0.
114

114

00:05:35,830  -->  00:05:37,890
So what have we done here?
115

115

00:05:37,890  -->  00:05:42,890
We first decided how we wanted to interact with our item.
116

116

00:05:43,520  -->  00:05:46,030
We wanted to be able to do item dot all
117

117

00:05:46,030  -->  00:05:47,270
to retrieve all the items
118

118

00:05:47,270  -->  00:05:51,210
because I feel that this is a pretty straightforward way of
119

119

00:05:51,210  -->  00:05:52,943
interacting with the database.
120

120

00:05:53,900  -->  00:05:57,030
Then, we wanted to be able to call the methods
121

121

00:05:57,030  -->  00:06:00,080
on each of the elements that this returns,
122

122

00:06:00,080  -->  00:06:04,240
which means this has to return a list of item objects.
123

123

00:06:04,240  -->  00:06:07,400
So then, we went and implemented that.
124

124

00:06:07,400  -->  00:06:08,630
And then I implemented this thing
125

125

00:06:08,630  -->  00:06:11,380
that allows us to retrieve everything from the database
126

126

00:06:11,380  -->  00:06:14,250
and gives us an item object for each.
127

127

00:06:14,250  -->  00:06:15,340
And as part of doing that,
128

128

00:06:15,340  -->  00:06:18,480
we also had to implement this find method
129

129

00:06:18,480  -->  00:06:21,150
in the database class, which just goes to the database
130

130

00:06:21,150  -->  00:06:22,973
and finds stuff and returns it.
131

131

00:06:24,350  -->  00:06:26,560
So I hope this was useful, I hope you learned something.
132

132

00:06:26,560  -->  00:06:28,110
I'll see you on the next video.
