1
1

00:00:01,129  -->  00:00:03,460
<v Jose>In this video we're going to be saving items</v>
2

2

00:00:03,460  -->  00:00:05,210
to MongoDB.
3

3

00:00:05,210  -->  00:00:06,380
The first thing we have to do
4

4

00:00:06,380  -->  00:00:08,260
is go into the item model
5

5

00:00:08,260  -->  00:00:12,050
and create our save_to_mongo method.
6

6

00:00:12,050  -->  00:00:15,090
What this is going to do is Database.insert
7

7

00:00:15,090  -->  00:00:17,010
and then just gonna pass in the collection
8

8

00:00:17,010  -->  00:00:21,070
and the result of calling this json method.
9

9

00:00:21,070  -->  00:00:22,657
So we're going to pass in the collection
10

10

00:00:22,657  -->  00:00:25,780
add a dictionary representing this item
11

11

00:00:25,780  -->  00:00:29,080
and the database module that we haven't created yet
12

12

00:00:29,080  -->  00:00:32,230
is going to be responsible for saving this data
13

13

00:00:32,230  -->  00:00:34,433
into this collection in MongoDB.
14

14

00:00:35,750  -->  00:00:37,730
So we can go over to our common package,
15

15

00:00:37,730  -->  00:00:39,280
create a new python file,
16

16

00:00:39,280  -->  00:00:44,090
and I'm going to call it database.py, like that.
17

17

00:00:44,090  -->  00:00:46,450
And what we're going to do in this database.py
18

18

00:00:46,450  -->  00:00:49,480
is create a class that I'm going to call Database,
19

19

00:00:49,480  -->  00:00:51,860
and in here, define two constants.
20

20

00:00:51,860  -->  00:00:54,607
The first one is going to be the URI,
21

21

00:00:55,570  -->  00:00:59,580
which is a connection string that MongoDB uses
22

22

00:00:59,580  -->  00:01:02,670
in order to define where we are connecting to.
23

23

00:01:02,670  -->  00:01:04,780
What server we're connecting to.
24

24

00:01:04,780  -->  00:01:06,880
If you're running MongoDB locally
25

25

00:01:06,880  -->  00:01:10,240
you're running the MongoD server in your computer
26

26

00:01:10,240  -->  00:01:14,680
then your URI is going to be almost identical to this one.
27

27

00:01:14,680  -->  00:01:19,087
It's going to be mongodb://127.0.0.1,
28

28

00:01:20,610  -->  00:01:23,893
make sure to not miss any periods or any numbers,
29

29

00:01:24,758  -->  00:01:27,990
:27017.
30

30

00:01:27,990  -->  00:01:32,040
It's almost definite that you will have to use this number.
31

31

00:01:32,040  -->  00:01:34,200
If you see a different number somewhere else
32

32

00:01:34,200  -->  00:01:35,140
don't worry too much about it.
33

33

00:01:35,140  -->  00:01:36,740
Do use this number first,
34

34

00:01:36,740  -->  00:01:38,670
because it's almost always this one.
35

35

00:01:38,670  -->  00:01:41,700
This is the port where the MongoDB server
36

36

00:01:41,700  -->  00:01:43,763
is listening for incoming connections.
37

37

00:01:45,000  -->  00:01:46,740
And then a forward slash,
38

38

00:01:46,740  -->  00:01:50,120
and the name of the database you want to use.
39

39

00:01:50,120  -->  00:01:52,630
In my case, I'm going to use one called pricing,
40

40

00:01:52,630  -->  00:01:54,393
but you can use anything else.
41

41

00:01:55,990  -->  00:01:56,823
Of course,
42

42

00:01:56,823  -->  00:01:58,490
do make sure that it's not one that already exists,
43

43

00:01:58,490  -->  00:02:01,120
since that might already have things in it
44

44

00:02:01,120  -->  00:02:03,990
and that could create confusion later on.
45

45

00:02:03,990  -->  00:02:06,410
Then we're going to create a database constant
46

46

00:02:06,410  -->  00:02:10,140
and that is going to be a pymongo MongoClient,
47

47

00:02:10,140  -->  00:02:12,560
where we're going to pass in this URI,
48

48

00:02:12,560  -->  00:02:15,930
and then we're gonna say get_database.
49

49

00:02:15,930  -->  00:02:17,923
We do have to import pymongo,
50

50

00:02:19,700  -->  00:02:21,850
and we do have to instal pymongo.
51

51

00:02:21,850  -->  00:02:23,640
So go to your PyCharm preferences,
52

52

00:02:23,640  -->  00:02:25,230
go to Project Interpreter,
53

53

00:02:25,230  -->  00:02:26,500
press the little plus icon,
54

54

00:02:26,500  -->  00:02:29,120
and find pymongo here,
55

55

00:02:29,120  -->  00:02:31,133
and then click Instal Package.
56

56

00:02:33,520  -->  00:02:34,620
Once that's installed,
57

57

00:02:34,620  -->  00:02:36,450
then your Pipfile and your Pipfile.lock files
58

58

00:02:37,907  -->  00:02:39,217
will have changed,
59

59

00:02:39,217  -->  00:02:42,527
and pymongo will be shown here in your settings
60

60

00:02:43,420  -->  00:02:45,210
and the red line will disappear
61

61

00:02:45,210  -->  00:02:46,173
and we're all good.
62

62

00:02:47,040  -->  00:02:50,350
The next thing is now that we have the database,
63

63

00:02:50,350  -->  00:02:53,660
we can insert things into it.
64

64

00:02:53,660  -->  00:02:56,520
The way we do that is we say Database,
65

65

00:02:56,520  -->  00:02:58,337
which is the class,
66

66

00:02:58,337  -->  00:02:59,170
.DATABASE,
67

67

00:03:00,460  -->  00:03:02,770
and then we tell it the collection
68

68

00:03:04,000  -->  00:03:06,650
and then we say .insert
69

69

00:03:06,650  -->  00:03:09,320
and then we tell it whatever we want to insert,
70

70

00:03:09,320  -->  00:03:11,913
like something like this, for example.
71

71

00:03:12,910  -->  00:03:17,910
Now we do want to be able to pass in the collection
72

72

00:03:17,970  -->  00:03:21,690
and the data, as to parameters to a function.
73

73

00:03:21,690  -->  00:03:22,523
So what we're going to do,
74

74

00:03:22,523  -->  00:03:26,960
is we're gonna put this inside an insert function here,
75

75

00:03:27,930  -->  00:03:28,950
or method,
76

76

00:03:28,950  -->  00:03:30,930
and we're going to pass in a collection
77

77

00:03:31,840  -->  00:03:33,870
and a data.
78

78

00:03:33,870  -->  00:03:37,500
And it's going to do Database.DATABASE[Collection],
79

79

00:03:37,500  -->  00:03:42,217
access this collection in our database
80

80

00:03:42,217  -->  00:03:43,127
.insert(data).
81

81

00:03:47,040  -->  00:03:50,310
This is going to allow us to take any data
82

82

00:03:50,310  -->  00:03:52,330
and put it into any collection
83

83

00:03:52,330  -->  00:03:56,390
and that is what the item is going to do.
84

84

00:03:56,390  -->  00:03:59,880
Now, an important thing is that this warning,
85

85

00:03:59,880  -->  00:04:03,117
it says, "Method 'insert' may be 'static'".
86

86

00:04:03,960  -->  00:04:05,270
So what does that mean?
87

87

00:04:05,270  -->  00:04:09,200
Well, at the moment this method has this self parameter
88

88

00:04:09,200  -->  00:04:11,570
which means that when we want to call
89

89

00:04:11,570  -->  00:04:14,470
this method from somewhere else in our code base,
90

90

00:04:14,470  -->  00:04:17,710
imagine we are in app.py or item.py
91

91

00:04:17,710  -->  00:04:20,053
what we'll have to do is something like this.
92

92

00:04:27,730  -->  00:04:28,779
Something like this.
93

93

00:04:28,779  -->  00:04:32,350
We will have to create a database object first
94

94

00:04:32,350  -->  00:04:34,700
because instance methods,
95

95

00:04:34,700  -->  00:04:36,870
those methods that have self,
96

96

00:04:36,870  -->  00:04:39,820
need to be called in an object,
97

97

00:04:39,820  -->  00:04:41,770
or on an object.
98

98

00:04:41,770  -->  00:04:43,580
So we don't want to do this.
99

99

00:04:43,580  -->  00:04:44,413
What we want to do
100

100

00:04:44,413  -->  00:04:47,610
is be able to call it directly on the class.
101

101

00:04:47,610  -->  00:04:48,610
Why?
102

102

00:04:48,610  -->  00:04:51,520
Well, quite simple, because it makes things easier.
103

103

00:04:51,520  -->  00:04:55,010
You don't have to create an object first which is not used,
104

104

00:04:55,010  -->  00:04:57,900
because PyCharm tells you that it could be static.
105

105

00:04:57,900  -->  00:04:59,040
It can be static,
106

106

00:04:59,040  -->  00:05:02,463
because self is not used anywhere inside the method.
107

107

00:05:03,440  -->  00:05:05,240
So let's make it static.
108

108

00:05:05,240  -->  00:05:06,660
The way you make a method static
109

109

00:05:06,660  -->  00:05:11,660
is you add a static method decorator like @staticmethod
110

110

00:05:12,010  -->  00:05:13,000
on top of it,
111

111

00:05:13,000  -->  00:05:14,690
and then you can delete self
112

112

00:05:14,690  -->  00:05:16,633
and just keep collection and data.
113

113

00:05:18,120  -->  00:05:19,840
Now for the purposes of type-hinting,
114

114

00:05:19,840  -->  00:05:22,650
we are going to define data to be a dict
115

115

00:05:22,650  -->  00:05:25,170
and collection to be a string.
116

116

00:05:25,170  -->  00:05:30,170
So we are going to say from typing import Dict
117

117

00:05:30,610  -->  00:05:33,683
as we will need that in there.
118

118

00:05:34,740  -->  00:05:36,750
Okay, now we have the insert method.
119

119

00:05:36,750  -->  00:05:38,470
We can go back to our item
120

120

00:05:38,470  -->  00:05:41,490
and we can actually insert it.
121

121

00:05:41,490  -->  00:05:44,420
So let's go into our import and say
122

122

00:05:44,420  -->  00:05:48,850
from common.database import Database,
123

123

00:05:48,850  -->  00:05:51,660
and now we can insert an item into MongoDB.
124

124

00:05:53,170  -->  00:05:54,100
Let's try it out.
125

125

00:05:54,100  -->  00:05:56,010
Let's go over to our app.py
126

126

00:05:56,010  -->  00:05:57,470
and I'm going to comment this stuff out
127

127

00:05:57,470  -->  00:06:00,130
that we wanted to do but we aren't ready to do yet,
128

128

00:06:00,130  -->  00:06:03,710
and I'm going to just keep the ipad.save_to_mongo.
129

129

00:06:03,710  -->  00:06:07,380
Run your app, make sure that you are running this file,
130

130

00:06:07,380  -->  00:06:09,010
and nothing will be printed out
131

131

00:06:09,010  -->  00:06:11,690
because we're not printing anything up here in the code,
132

132

00:06:11,690  -->  00:06:13,630
but the process should finish
133

133

00:06:13,630  -->  00:06:16,380
as long as you have MongoDB running,
134

134

00:06:16,380  -->  00:06:19,760
and we can go over to MongoDB and check it out.
135

135

00:06:19,760  -->  00:06:22,070
Check if we have something here.
136

136

00:06:22,070  -->  00:06:24,360
So we're going to say use pricing.
137

137

00:06:24,360  -->  00:06:26,890
This is the database that we have selected
138

138

00:06:26,890  -->  00:06:28,610
in our database module,
139

139

00:06:28,610  -->  00:06:33,130
and we're gonna say db.items.find({}).pretty().
140

140

00:06:33,130  -->  00:06:37,327
Something like this, db.items, gives us the collection.
141

141

00:06:37,327  -->  00:06:41,320
.find is the method you call on a collection
142

142

00:06:41,320  -->  00:06:43,300
to find something.
143

143

00:06:43,300  -->  00:06:46,520
Pass in an empty object there, an empty JavaScript object
144

144

00:06:46,520  -->  00:06:47,800
or empty dictionary,
145

145

00:06:47,800  -->  00:06:50,520
in order to find all the items,
146

146

00:06:50,520  -->  00:06:52,260
and then .pretty at the end
147

147

00:06:52,260  -->  00:06:54,190
just to format them a bit more nicely.
148

148

00:06:54,190  -->  00:06:57,090
As you can see, it prints them out like this.
149

149

00:06:57,090  -->  00:06:58,090
And there you have it.
150

150

00:06:58,090  -->  00:07:00,300
We have our stuff.
151

151

00:07:00,300  -->  00:07:02,320
We have our data in MongoDB.
152

152

00:07:02,320  -->  00:07:07,320
We have the ID that we generated, a UUID Type 4.
153

153

00:07:07,420  -->  00:07:09,170
We have the URL that we selected,
154

154

00:07:09,170  -->  00:07:11,060
we have the tag name which is "p",
155

155

00:07:11,060  -->  00:07:15,790
and we have our query which is class, price price--large.
156

156

00:07:15,790  -->  00:07:19,050
So now you can see how we can then
157

157

00:07:19,050  -->  00:07:20,630
as we save more items,
158

158

00:07:20,630  -->  00:07:23,480
but eventually come back and check all the prices at once
159

159

00:07:23,480  -->  00:07:26,133
because they're all going to be saved in MongoDB.
160

160

00:07:26,990  -->  00:07:29,530
The next thing is of course to do that.
161

161

00:07:29,530  -->  00:07:31,790
We're gonna be able to retrieve all of the items
162

162

00:07:31,790  -->  00:07:33,240 line:15% 
from the database,
163

163

00:07:33,240  -->  00:07:36,300 line:15% 
and check their prices, or check their names,
164

164

00:07:36,300  -->  00:07:38,360 line:15% 
or whatever else we want to do.
165

165

00:07:38,360  -->  00:07:40,670 line:15% 
We're going to be implementing this in the next video,
166

166

00:07:40,670  -->  00:07:41,770
so I'll see you there.
