NOTE
This file was generated by Descript <www.descript.com>
1

00:00:00,050  -->  00:00:01,580
Hi guys and welcome back.

2

00:00:01,609  -->  00:00:04,987
In this video, let's talk
about SQL databases and Mongo

3

00:00:04,987  -->  00:00:06,577
DB and how they are different.

4

00:00:07,054  -->  00:00:10,264
The usual databases are built from tables.

5

00:00:10,874  -->  00:00:15,584
SQL databases that use the SQL query
language which we will learn a little

6

00:00:15,604  -->  00:00:18,379
bit about in a moment use tables as well.

7

00:00:18,379  -->  00:00:20,239
So they are the usual databases.

8

00:00:20,641  -->  00:00:24,841
In SQL databases, you build
relationships between tables via

9

00:00:24,841  -->  00:00:27,811
the use of primary and foreign keys.

10

00:00:28,321  -->  00:00:33,361
In this table, which describes users,
the id column would be the primary key.

11

00:00:33,811  -->  00:00:36,901
And you could have something like
their bank accounts which has a

12

00:00:36,901  -->  00:00:38,901
foreign key which is the user_id.

13

00:00:39,331  -->  00:00:44,521
So with SQL, with the query language,
you could then get information from

14

00:00:44,521  -->  00:00:47,131
both tables by using this relationship.

15

00:00:47,634  -->  00:00:51,954
Even though the relationship may not
necessarily be, you know, hard coded

16

00:00:52,134  -->  00:00:56,934
in the code that relationship exists
because the user ID in one table should

17

00:00:56,934  -->  00:00:59,184
be the same as the ID in another table.

18

00:00:59,274  -->  00:01:03,564
So you can use SQL, which looks,
something like that to select information

19

00:01:03,564  -->  00:01:04,824
from both tables at the same time.

20

00:01:05,341  -->  00:01:07,711
But NoSQL is a bit different.

21

00:01:08,251  -->  00:01:13,031
In NoSQL, we don't use the SQL
query language, which is the code

22

00:01:13,081  -->  00:01:14,431
that you just saw a moment ago.

23

00:01:14,994  -->  00:01:19,104
Instead the different databases,
mongo DB is one of them, use

24

00:01:19,104  -->  00:01:21,354
different ways of querying that data.

25

00:01:21,868  -->  00:01:25,108
Also databases store
data in different ways.

26

00:01:25,468  -->  00:01:30,808
And this is because each way of storing
data provides some different benefits.

27

00:01:31,393  -->  00:01:34,783
However each way of storing
data also has some drawbacks.

28

00:01:34,993  -->  00:01:40,693
So you can pick a different NoSQL database
depending on what benefits you want and

29

00:01:40,693  -->  00:01:42,793
what drawbacks you're willing to accept.

30

00:01:43,332  -->  00:01:46,022
MongoDB doesn't use tables with columns.

31

00:01:46,172  -->  00:01:50,582
That are some NoSQL databases that use
tables with columns but not very many.

32

00:01:51,122  -->  00:01:54,572
Instead MongoDB uses
collections with documents.

33

00:01:54,902  -->  00:01:58,862
And you might be thinking, okay, that's
just semantics surely they are the same.

34

00:01:59,222  -->  00:02:00,302
But they're not quite the same.

35

00:02:00,752  -->  00:02:03,962
The key difference is that when
you have a table with columns all

36

00:02:03,962  -->  00:02:06,632
the rows contain the same fields.

37

00:02:06,932  -->  00:02:10,382
Let's say you have three
columns and you have three rows.

38

00:02:10,562  -->  00:02:14,012
You know that the three rows
each have three values in them.

39

00:02:14,552  -->  00:02:17,749
However, MongoDB documents
can contain whatever you want.

40

00:02:18,439  -->  00:02:22,459
So they don't have to have the
same stuff in them as other

41

00:02:22,459  -->  00:02:24,349
documents in the same collection.

42

00:02:24,499  -->  00:02:27,642
So your collections could be
made up of disparate things.

43

00:02:28,199  -->  00:02:29,759
Now that might look a bit weird.

44

00:02:29,759  -->  00:02:33,769
It might feel wrong but it's one
of the key elements of MongoDB.

45

00:02:33,869  -->  00:02:35,039
It's that flexibility.

46

00:02:35,429  -->  00:02:37,439
So what does a document look like?

47

00:02:37,469  -->  00:02:38,639
If it's not a table.

48

00:02:39,142  -->  00:02:41,692
You document is actually
something like this.

49

00:02:41,722  -->  00:02:43,702
It is basically JSON.

50

00:02:44,092  -->  00:02:47,302
And if you don't know what JSON is,
then you're looking at it right now.

51

00:02:47,382  -->  00:02:50,992
JSON is a collection of
field names and values.

52

00:02:51,502  -->  00:02:55,822
The ID or underscore ID field is a
required field inMongoDB documents.

53

00:02:56,257  -->  00:02:58,927
You can have things like
embedded sub documents.

54

00:02:59,317  -->  00:03:01,117
And this here is a field.

55

00:03:01,147  -->  00:03:05,260
You can see the name goes before the colon
and after the colon, you've got the value.

56

00:03:05,757  -->  00:03:10,257
The embedded sub documents are
just documents that are a value

57

00:03:10,257  -->  00:03:12,027
to a field in another document.

58

00:03:12,527  -->  00:03:17,777
You can see that the values of the fields
can be strings or integers but there's

59

00:03:17,777  -->  00:03:20,207
also a bunch of other types of value.

60

00:03:20,297  -->  00:03:25,217
For example, the underscore ID field
has a value of type object ID which

61

00:03:25,217  -->  00:03:26,807
is something that's unique to MongoDB.

62

00:03:27,167  -->  00:03:30,767
And that identifies the
different documents uniquely.

63

00:03:31,264  -->  00:03:33,814
We're going to learn more about
what sort of stuff we can store in

64

00:03:33,814  -->  00:03:35,674
MongoDB as we go through the course.

65

00:03:36,151  -->  00:03:40,561
So I said earlier that NoSQL
databases have benefits and drawbacks.

66

00:03:40,561  -->  00:03:42,731
Let's talk about some of
the benefits of MongoDB.

67

00:03:43,237  -->  00:03:46,027
The first one is you
don't have a fixed schema.

68

00:03:46,057  -->  00:03:48,727
You don't have these fixed
columns in each table.

69

00:03:48,847  -->  00:03:50,437
So you have a bit more flexibility.

70

00:03:50,437  -->  00:03:53,737
You can just store whatever
you want inside each document.

71

00:03:54,264  -->  00:03:58,254
And this can lead to faster development
times because you don't have to worry

72

00:03:58,254  -->  00:04:02,761
about changing your database every time
you want to add a different column or

73

00:04:02,761  -->  00:04:05,071
a different field to your documents.

74

00:04:05,251  -->  00:04:08,521
So you can just go ahead and do it
without having to go into your database,

75

00:04:08,521  -->  00:04:13,491
make changes there and then restart your
database or potentially delete other rows

76

00:04:13,491  -->  00:04:15,441
that didn't have those fields, et cetera.

77

00:04:15,996  -->  00:04:19,326
The data storage in MongoDB,
which is JSON, is very similar

78

00:04:19,326  -->  00:04:20,586
to Python dictionaries.

79

00:04:20,766  -->  00:04:25,146
So when you extract data from MongoDB,
it's very easy to automatically

80

00:04:25,146  -->  00:04:26,676
convert it to Python dictionaries.

81

00:04:26,856  -->  00:04:32,706
And you don't need to map data from the
database into a native data structure.

82

00:04:33,231  -->  00:04:36,651
Also something I particularly like
about Mongo DB is the scalability

83

00:04:36,651  -->  00:04:38,301
model is quite straightforward.

84

00:04:38,481  -->  00:04:42,981
So when you reach the limit for
what a single MongoDB database can

85

00:04:42,981  -->  00:04:47,781
do, it's relatively easy to use two
databases that are linked together.

86

00:04:48,317  -->  00:04:51,677
Although we won't get to this part of
MongoDB in this course, it's something

87

00:04:51,677  -->  00:04:53,177
that you can do relatively easily.

88

00:04:53,397  -->  00:04:58,187
MongoDB University has some free courses
that you can take on MongoDB scalability

89

00:04:58,307  -->  00:04:59,237
and they're pretty good as well.

90

00:04:59,767  -->  00:05:05,194
That are drawbacks to MongoDB and the main
drawbacks are the main strengths of SQL.

91

00:05:05,734  -->  00:05:08,494
SQL is popular and it
is popular for a reason.

92

00:05:08,991  -->  00:05:13,801
The first one is that SQL has JOINs
and MongoDB does not have JOINs.

93

00:05:14,316  -->  00:05:17,136
What that means is using
SQL and a SQL database.

94

00:05:17,256  -->  00:05:20,406
You can select data from two
or more tables at the same

95

00:05:20,406  -->  00:05:22,776
time using the database code.

96

00:05:22,986  -->  00:05:24,606
And that is very fast.

97

00:05:24,906  -->  00:05:26,736
In MongoDB, you can't do that.

98

00:05:26,736  -->  00:05:31,326
You have to select manually using
the Python code and therefore the

99

00:05:31,326  -->  00:05:35,386
selection of two tables or more
at the same time is much slower.

100

00:05:35,882  -->  00:05:40,022
MongoDB can use more memory because
you have to store the key name

101

00:05:40,022  -->  00:05:41,852
for each field in every document.

102

00:05:42,032  -->  00:05:46,202
Whereas when you use SQL, the column
names are only stored once for each table.

103

00:05:46,707  -->  00:05:49,567
As usual there are limits to the
size of documents and the amount

104

00:05:49,567  -->  00:05:50,737
of nesting within documents.

105

00:05:50,947  -->  00:05:53,377
But that is also true of SQL databases.

106

00:05:53,377  -->  00:05:55,927
So this is not a big drawback, but
it's something to take into account.

107

00:05:56,445  -->  00:06:00,465
Something that you can run across when
using MongoDB is that if you store

108

00:06:00,735  -->  00:06:05,942
a lot of high size things in your
documents, MongoDB slows down massively.

109

00:06:05,972  -->  00:06:10,442
For example, if you put PDFs or images
in your documents that can result

110

00:06:10,442  -->  00:06:13,118
in much slower speed for MongoDB.

111

00:06:13,627  -->  00:06:17,277
This is not a comprehensive list of
the benefits and drawbacks of MongoDB.

112

00:06:17,407  -->  00:06:18,517
These are just the main ones.

113

00:06:18,757  -->  00:06:23,737
But there are other things that MongoDB
has as edge cases that you should be

114

00:06:23,737  -->  00:06:28,297
aware of when you start using MongoDB
in a high performance environment.

115

00:06:28,825  -->  00:06:31,375
However, I can tell you that I've
used MongoDB in a high-performance

116

00:06:31,375  -->  00:06:33,655
environment and it works very well.

117

00:06:33,745  -->  00:06:37,405
So it's always going to come down to
exactly what you're doing with your code.

118

00:06:37,405  -->  00:06:40,565
Whether you should choose MongoDB,
or a slightly different NoSQL

119

00:06:40,585  -->  00:06:43,285
database, or even a SQL database.

120

00:06:43,762  -->  00:06:44,152
All right.

121

00:06:44,152  -->  00:06:45,832
Thank you guys for
joining me in this video.

122

00:06:45,982  -->  00:06:48,412
I hope you've learned something
and I'll see you in the next one.
