NOTE
This file was generated by Descript <www.descript.com>
1

00:00:00,000  -->  00:00:01,500
Hi guys and welcome back.

2

00:00:01,530  -->  00:00:05,969
In this video, we're going to start
saving our microblog entries into MongoDB.

3

00:00:06,570  -->  00:00:08,219
So make sure you've got your project open.

4

00:00:08,459  -->  00:00:13,770
Your website open and MongoDB Compass
open to the entries collection in your

5

00:00:13,830  -->  00:00:16,560
microblog database in MongoDB Compass.

6

00:00:16,770  -->  00:00:17,760
So that was a mouthful.

7

00:00:18,090  -->  00:00:19,680
Let's get started with this video.

8

00:00:20,601  -->  00:00:23,991
The first thing we want to do, is go
over to our editor and we're going

9

00:00:23,991  -->  00:00:25,880
to activate the virtual environment.

10

00:00:25,971  -->  00:00:26,840
I've already done that.

11

00:00:26,871  -->  00:00:27,801
Make sure you do that.

12

00:00:28,151  -->  00:00:30,730
And then type pip install, pymongo[srv].

13

00:00:30,970  -->  00:00:36,581
You can install, pymongo on its
own, but with the srv in square

14

00:00:36,581  -->  00:00:40,360
brackets there, it'll also install
some extra package that we need in

15

00:00:40,360  -->  00:00:42,581
order to connect to MongoDB Atlas.

16

00:00:43,120  -->  00:00:45,501
And that package is called dnspython.

17

00:00:45,521  -->  00:00:47,991
So it installs pymongo and dnspython.

18

00:00:48,400  -->  00:00:49,631
And now we can connect.

19

00:00:50,158  -->  00:00:54,397
Now that we've got that we want to
go ahead and import from pymongo.

20

00:00:54,468  -->  00:00:58,168
We'll do from pymongo import MongoClient.

21

00:00:58,598  -->  00:01:04,328
And that is going to allow us to open up
a client side session to MongoDB that we

22

00:01:04,328  -->  00:01:06,578
can then use to connect to our database.

23

00:01:07,111  -->  00:01:09,721
Once we've imported that
just underneath app.py.

24

00:01:10,111  -->  00:01:14,476
We're going to do client equal
MongoClient and in here, we're going to

25

00:01:14,476  -->  00:01:18,156
pass in the MongoDB connection string.

26

00:01:18,561  -->  00:01:21,741
So that's this one here and make
sure to put in your password as

27

00:01:21,741  -->  00:01:23,161
well which I'm going to do just now.

28

00:01:23,652  -->  00:01:24,422
Once you've got that.

29

00:01:24,512  -->  00:01:29,522
We have a client, but we haven't
connected to any database yet.

30

00:01:29,882  -->  00:01:33,452
So what we're going to do now is we're
going to connect to the database.

31

00:01:33,452  -->  00:01:40,112
So we'll do db is client.microblog, and
that is the microblog database that we've

32

00:01:40,112  -->  00:01:43,192
already created over in our cluster.

33

00:01:43,557  -->  00:01:48,280
You can think of the MongoClient as the
cluster representation and client.mongodb

34

00:01:48,520  -->  00:01:50,410
actually connects to the database.

35

00:01:50,920  -->  00:01:55,870
It's not good form, really to store
all of these database connections

36

00:01:55,870  -->  00:01:58,830
inside the main app.py file.

37

00:01:58,950  -->  00:02:01,920
And I'll get into why that is
much later on in the course.

38

00:02:02,160  -->  00:02:07,316
But what we want to do here is we want to
put this database value inside the app.

39

00:02:07,376  -->  00:02:12,049
So we'll do app dot db, and that
is going to essentially save this

40

00:02:12,079  -->  00:02:14,269
microblog connection inside app.

41

00:02:14,786  -->  00:02:18,573
And later on, we'll be creating this
app in a bit of a more dynamic way

42

00:02:18,723  -->  00:02:20,553
that will give us fewer problems.

43

00:02:20,733  -->  00:02:24,495
Again, I know that this is all
very unclear at this point, but

44

00:02:24,555  -->  00:02:27,015
it's what we have to do because
it is a bit more complicated.

45

00:02:27,286  -->  00:02:30,996
Once we've got this, we now have
the database saved in our app.

46

00:02:31,356  -->  00:02:33,366
So what we can do is we can come in here.

47

00:02:33,676  -->  00:02:39,406
For example do app.db and this accesses
the microblog database and we can

48

00:02:39,406  -->  00:02:43,786
do .entries and that accesses the
entries collection in our database.

49

00:02:44,206  -->  00:02:48,586
And we can do something like .find
and put in an empty dictionary.

50

00:02:49,021  -->  00:02:50,161
That's an empty dictionary there.

51

00:02:50,401  -->  00:02:54,511
What that's going to do, it is going to
find everything in our entries collection

52

00:02:54,721  -->  00:02:56,461
and it's going to give it back to us.

53

00:02:56,866  -->  00:03:00,436
We're going to print and then we're
going to create a list comprehension

54

00:03:00,496  -->  00:03:04,786
that says e for e in app.db.entries.find.

55

00:03:05,236  -->  00:03:08,446
So we're just going to print everything
that we find in the database.

56

00:03:08,506  -->  00:03:09,376
This is what this does.

57

00:03:10,076  -->  00:03:11,459
Let's save that.

58

00:03:11,729  -->  00:03:15,079
And now we can go ahead
and run the Flask app.

59

00:03:15,079  -->  00:03:16,259
So we will do Flask run.

60

00:03:16,726  -->  00:03:17,836
And they should just work.

61

00:03:17,866  -->  00:03:20,596
If you do get an error here,
make sure that you've got

62

00:03:20,816  -->  00:03:23,506
pymongo and dnspython installed.

63

00:03:23,686  -->  00:03:25,936
And how you can check that you've
got both of those installed

64

00:03:25,996  -->  00:03:27,766
is you can do pip freeze.

65

00:03:28,006  -->  00:03:30,706
And that should tell you everything
that you've got installed and pymongo

66

00:03:30,706  -->  00:03:33,126
should be there and so should dnspython.

67

00:03:33,650  -->  00:03:36,259
After that, you can go over
to your website and refresh

68

00:03:36,259  -->  00:03:40,009
the page and you should see an
empty list being printed out.

69

00:03:40,313  -->  00:03:44,106
So let's go over to Compass
and insert a test document.

70

00:03:44,106  -->  00:03:47,356
I'm just going to put something
here that says name is test.

71

00:03:47,819  -->  00:03:51,089
Make sure to create it correctly
there with the colon, the two strings

72

00:03:51,089  -->  00:03:52,799
on each side and the curly braces.

73

00:03:52,979  -->  00:03:54,269
We're going to insert this.

74

00:03:54,779  -->  00:03:57,869
Then we're going to go back to our
website and refresh again and without

75

00:03:57,869  -->  00:04:01,829
restarting the app, you should see
the new thing printed out there.

76

00:04:02,313  -->  00:04:06,743
This is one great things of using a
database, as opposed to a Python list.

77

00:04:06,953  -->  00:04:08,313
You don't have to restart your app.

78

00:04:08,353  -->  00:04:12,643
You don't have to change anything because
here the query is finding data in the

79

00:04:12,643  -->  00:04:15,163
database at this specific point in time.

80

00:04:15,253  -->  00:04:18,763
So whenever we make updates to the
database, those are going to be

81

00:04:18,763  -->  00:04:20,300
reflected on our app immediately.

82

00:04:20,557  -->  00:04:24,747
I told you at the beginning of this video
that we weren't going to be retrieving

83

00:04:24,747  -->  00:04:26,367
our entries from MongoDB in this video.

84

00:04:26,638  -->  00:04:27,477
So we're going to delete that.

85

00:04:27,567  -->  00:04:31,078
What we're going to focus on
is on inserting the entries.

86

00:04:31,407  -->  00:04:35,181
To insert our entries, we're going to go
directly here underneath entries.append

87

00:04:35,451  -->  00:04:41,266
and we're going to do app.db.entries,
that accesses the entries collection

88

00:04:41,266  -->  00:04:47,386
in our database, .insert, and now we
have to insert a Python dictionary

89

00:04:47,716  -->  00:04:51,556
and pymongo which is the library we've
installed will take care of turning that

90

00:04:51,556  -->  00:04:54,037
into JSON and putting it into MongoDB.

91

00:04:54,436  -->  00:04:58,037
So what we're going to put in here
is a dictionary that has content of

92

00:04:58,316  -->  00:05:02,414
entry_content and date of formatted_date.

93

00:05:02,933  -->  00:05:06,084
So once we've done this we
do have to restart our app.

94

00:05:06,268  -->  00:05:07,424
Let me just do that.

95

00:05:07,575  -->  00:05:09,435
I'll turn that off and
turn it back on again.

96

00:05:09,902  -->  00:05:13,952
And we can go over to the microblog
and enter something in here.

97

00:05:13,982  -->  00:05:16,112
Let me just refresh the
page real quick first.

98

00:05:16,412  -->  00:05:20,432
And in the entry contents, we're going
to put in something like Hello, world!

99

00:05:20,522  -->  00:05:23,952
I am saving into MongoDB.

100

00:05:24,453  -->  00:05:25,412
So we add the entry.

101

00:05:25,936  -->  00:05:29,615
Noted that, the entry does appear
here, but that's because as

102

00:05:29,615  -->  00:05:31,095
well as inserting into MongoDB.

103

00:05:31,265  -->  00:05:34,355
We've saved it into an entries
list and then we're displaying

104

00:05:34,355  -->  00:05:35,795
the things from the entries list.

105

00:05:36,095  -->  00:05:39,688
So this here is not
coming from MongoDB yet.

106

00:05:39,779  -->  00:05:41,009
It will do that in the next video.

107

00:05:41,279  -->  00:05:45,719
What we want to do now though, is check
that MongoDB Compass does have our data.

108

00:05:46,009  -->  00:05:48,139
So I've gone over to MongoDB Compass.

109

00:05:48,289  -->  00:05:52,818
And if it doesn't appear just like
refresh and it should appear there.

110

00:05:52,878  -->  00:05:56,769
You should see the content, the
date and the unique object ID.

111

00:05:57,299  -->  00:06:00,469
At this point you should delete
the old test entry because

112

00:06:00,469  -->  00:06:01,459
we don't need that anymore.

113

00:06:01,459  -->  00:06:05,388
And later on, it may be confusing to have
some documents with certain fields and

114

00:06:05,388  -->  00:06:07,489
another document with a test field there.

115

00:06:08,067  -->  00:06:08,397
All right.

116

00:06:08,397  -->  00:06:09,627
That is everything for this video.

117

00:06:09,627  -->  00:06:12,017
We've managed to save
our stuff into MongoDB.

118

00:06:12,297  -->  00:06:14,927
Now all that's left is to
retrieve it from MongoDB.

119

00:06:14,927  -->  00:06:16,527
And we already know how to do that.

120

00:06:16,527  -->  00:06:19,077
So I'd encourage you to
try and give that a go.

121

00:06:19,257  -->  00:06:24,837
Use the find method on our entries
collection on pymongo to retrieve the

122

00:06:24,837  -->  00:06:27,357
data and try to display it in the website.

123

00:06:27,357  -->  00:06:28,227
See if you manage that.

124

00:06:28,437  -->  00:06:29,397
I'm sure you can do it.

125

00:06:29,812  -->  00:06:33,262
And if not, let's go over to the next
video and I'll show you how to do that.

126

00:06:33,682  -->  00:06:34,252
I'll see you there.
