1
00:00:01,631 --> 00:00:03,364
So now, it's finally time

2
00:00:03,364 --> 00:00:07,249
to get started writing some MongoDB code

3
00:00:07,249 --> 00:00:09,640
or running some MongoDB queries.

4
00:00:09,640 --> 00:00:11,070
And as you'll see,

5
00:00:11,070 --> 00:00:13,270
that will be quite different to what we did

6
00:00:13,270 --> 00:00:14,701
in the SQL world.

7
00:00:14,701 --> 00:00:19,316
When it came to working with SQL and MySQL,

8
00:00:19,316 --> 00:00:23,240
we started by setting up a database and then some tables,

9
00:00:23,240 --> 00:00:27,379
and we started by setting up the structure of our table.

10
00:00:27,379 --> 00:00:31,101
Now, here with MongoDB, it will be quite different.

11
00:00:31,101 --> 00:00:35,493
If we wanna insert some data, if we wanna store some data,

12
00:00:35,493 --> 00:00:38,450
we don't need to set up any table at all,

13
00:00:38,450 --> 00:00:41,930
simply because, with MongoDB,

14
00:00:41,930 --> 00:00:46,210
or with NoSQL databases in general, as you learned,

15
00:00:46,210 --> 00:00:48,605
the idea of having a fixed structure

16
00:00:48,605 --> 00:00:52,930
with predefined data types just doesn't exist.

17
00:00:52,930 --> 00:00:54,410
Instead, as you learned,

18
00:00:54,410 --> 00:00:56,878
the idea is that you have these collections

19
00:00:56,878 --> 00:00:58,584
with documents inside of them,

20
00:00:58,584 --> 00:01:01,043
and that you can have different documents

21
00:01:01,043 --> 00:01:05,689
with different structures in one of the same collection.

22
00:01:05,689 --> 00:01:08,533
Now, in reality, you will typically end up

23
00:01:08,533 --> 00:01:12,532
with similarly structured documents in the same collection,

24
00:01:12,532 --> 00:01:15,913
but you simply don't have a fixed predefined structure

25
00:01:15,913 --> 00:01:18,196
for your documents in a collection.

26
00:01:18,196 --> 00:01:19,977
So therefore, with MongoDB,

27
00:01:19,977 --> 00:01:22,879
we don't start by setting up tables or collections,

28
00:01:22,879 --> 00:01:25,199
or by defining some structures,

29
00:01:25,199 --> 00:01:28,921
we can just start by inserting some data.

30
00:01:28,921 --> 00:01:30,980
As a first step though,

31
00:01:30,980 --> 00:01:33,631
we have to select a database

32
00:01:33,631 --> 00:01:36,360
into which we wanna select data.

33
00:01:36,360 --> 00:01:39,017
And we ran the show dbs command

34
00:01:39,017 --> 00:01:41,300
to see which databases we have.

35
00:01:41,300 --> 00:01:43,230
But these three databases,

36
00:01:43,230 --> 00:01:45,476
which are listed here out of the box for me,

37
00:01:45,476 --> 00:01:49,658
are all built-in databases that store some system data

38
00:01:49,658 --> 00:01:50,900
in some configuration,

39
00:01:50,900 --> 00:01:53,530
and we therefore don't wanna use them

40
00:01:53,530 --> 00:01:55,561
for our application data.

41
00:01:55,561 --> 00:01:57,465
Instead, what we wanna do here

42
00:01:57,465 --> 00:02:00,611
is we wanna use a new database.

43
00:02:00,611 --> 00:02:03,500
And for this, we can run the use command here.

44
00:02:03,500 --> 00:02:04,346
Simply type use,

45
00:02:04,346 --> 00:02:07,442
and then the database name you wanna work with.

46
00:02:07,442 --> 00:02:10,288
And as an example for this section,

47
00:02:10,288 --> 00:02:12,990
I'll come back to an example I used

48
00:02:12,990 --> 00:02:15,098
in the SQL introduction section,

49
00:02:15,098 --> 00:02:17,333
where we had Restaurants and Reviews,

50
00:02:17,333 --> 00:02:21,126
simply because there I showed you how you would store

51
00:02:21,126 --> 00:02:23,590
that kind of data with SQL.

52
00:02:23,590 --> 00:02:24,500
Now, I think it's great

53
00:02:24,500 --> 00:02:27,217
to understand how we would store that with NoSQL.

54
00:02:27,217 --> 00:02:30,501
But again, we'll not start with the full example here.

55
00:02:30,501 --> 00:02:33,200
We'll come to that later in this section,

56
00:02:33,200 --> 00:02:35,962
instead I wanna start with the basic CRUD operations.

57
00:02:35,962 --> 00:02:38,070
And hence, we'll just have a look

58
00:02:38,070 --> 00:02:41,264
at some dummy Restaurants data that we might wanna store

59
00:02:41,264 --> 00:02:43,562
in a Restaurants collection.

60
00:02:43,562 --> 00:02:46,450
So first of all, we need a database as mentioned,

61
00:02:46,450 --> 00:02:48,290
and we can use the use command for this,

62
00:02:48,290 --> 00:02:50,424
and then a fitting database name.

63
00:02:50,424 --> 00:02:53,402
So not a collection name for the restaurants,

64
00:02:53,402 --> 00:02:55,770
but the name for a database

65
00:02:55,770 --> 00:02:58,640
that might hold multiple collections related

66
00:02:58,640 --> 00:03:01,050
to restaurants and reviews.

67
00:03:01,050 --> 00:03:03,317
It could be ratingportal,

68
00:03:03,317 --> 00:03:06,640
because maybe we're building a portal for users

69
00:03:06,640 --> 00:03:08,587
to leave ratings.

70
00:03:08,587 --> 00:03:11,290
So now you can hit Enter, and that's all.

71
00:03:11,290 --> 00:03:12,309
As you can tell,

72
00:03:12,309 --> 00:03:15,867
we now switched to this new database here.

73
00:03:15,867 --> 00:03:18,410
And of course, that wasn't the database we had before,

74
00:03:18,410 --> 00:03:22,184
but by simply using it, it was created for us.

75
00:03:22,184 --> 00:03:25,950
You still don't see it if you run show dbs,

76
00:03:25,950 --> 00:03:29,125
because it will actually only really be created

77
00:03:29,125 --> 00:03:32,900
once you start inserting some data into a collection

78
00:03:32,900 --> 00:03:34,047
in that database.

79
00:03:34,047 --> 00:03:38,468
But switching to it is the first step for doing so.

80
00:03:38,468 --> 00:03:41,458
Now once you've switched,

81
00:03:41,458 --> 00:03:44,380
you can start inserting data.

82
00:03:44,380 --> 00:03:45,370
Because as I mentioned,

83
00:03:45,370 --> 00:03:49,140
you don't need to create a table or collection to do so,

84
00:03:49,140 --> 00:03:51,310
instead it will be created on the fly

85
00:03:51,310 --> 00:03:53,137
if you simply insert data.

86
00:03:53,137 --> 00:03:55,814
Now to insert data into a collection,

87
00:03:55,814 --> 00:04:00,168
we write db to refer to the currently active database.

88
00:04:00,168 --> 00:04:03,020
And then, we need to tell MongoDB

89
00:04:03,020 --> 00:04:04,909
into which collection we wanna insert data.

90
00:04:04,909 --> 00:04:07,334
For example, the restaurant's collection.

91
00:04:07,334 --> 00:04:10,374
So you just type the restaurants collection

92
00:04:10,374 --> 00:04:13,609
after a dot after db.

93
00:04:13,609 --> 00:04:16,730
Now, this collection doesn't exist yet,

94
00:04:16,730 --> 00:04:19,647
but just like the database, it will be created on the fly

95
00:04:19,647 --> 00:04:21,649
once we insert data.

96
00:04:21,649 --> 00:04:22,769
And as a side note,

97
00:04:22,769 --> 00:04:25,442
this already looks a lot like JavaScript.

98
00:04:25,442 --> 00:04:28,870
db.restaurants, that looks

99
00:04:28,870 --> 00:04:32,800
like we always access properties on JavaScript objects

100
00:04:32,800 --> 00:04:35,901
throughout this course, with this dot notation.

101
00:04:35,901 --> 00:04:38,902
And indeed, there is a lot of similarity.

102
00:04:38,902 --> 00:04:40,945
The MongoDB query language,

103
00:04:40,945 --> 00:04:42,450
which we are using here,

104
00:04:42,450 --> 00:04:44,880
is based on JavaScript, you could say,

105
00:04:44,880 --> 00:04:47,060
but it's really important to understand

106
00:04:47,060 --> 00:04:48,977
that this is a specific language written

107
00:04:48,977 --> 00:04:53,120
by the MongoDB team, or created by the MongoDB team,

108
00:04:53,120 --> 00:04:55,180
understood by the MongoDB server

109
00:04:55,180 --> 00:04:58,204
that really can only be used with MongoDB.

110
00:04:58,204 --> 00:05:00,799
Because that's a key difference compared

111
00:05:00,799 --> 00:05:03,527
to SQL-based database systems.

112
00:05:03,527 --> 00:05:08,527
Whereas SQL is a generally standardized language

113
00:05:09,560 --> 00:05:13,068
that works with all SQL-based database systems,

114
00:05:13,068 --> 00:05:15,700
NoSQL database systems

115
00:05:15,700 --> 00:05:18,790
typically bring their own query language.

116
00:05:18,790 --> 00:05:22,631
So this language here works with MongoDB only in the end,

117
00:05:22,631 --> 00:05:24,618
but the documentation

118
00:05:24,618 --> 00:05:27,927
of the different database engines is really good in the end

119
00:05:27,927 --> 00:05:30,080
and it's all straightforward

120
00:05:30,080 --> 00:05:32,106
to pick up those query languages.

121
00:05:32,106 --> 00:05:35,105
So here, with that, we access a collection

122
00:05:35,105 --> 00:05:37,828
on the database we are currently using,

123
00:05:37,828 --> 00:05:41,989
and then we can type another dot and now execute a bunch

124
00:05:41,989 --> 00:05:45,580
of methods, you could say, to run queries

125
00:05:45,580 --> 00:05:47,881
against this collection in this database.

126
00:05:47,881 --> 00:05:50,823
For example, the insertOne method.

127
00:05:50,823 --> 00:05:54,180
And again, this looks just like JavaScript.

128
00:05:54,180 --> 00:05:57,850
We're now executing this method on this collection.

129
00:05:57,850 --> 00:05:59,330
And as the name suggests,

130
00:05:59,330 --> 00:06:02,370
insertOne can be used to insert data,

131
00:06:02,370 --> 00:06:05,705
one piece of data, to be precise, into this collection.

132
00:06:05,705 --> 00:06:09,280
There also is a insertMany command

133
00:06:09,280 --> 00:06:12,070
in case you wanna bulk insert data,

134
00:06:12,070 --> 00:06:14,450
and insert multiple documents at once.

135
00:06:14,450 --> 00:06:17,060
But we're going to get started in a simple way here

136
00:06:17,060 --> 00:06:18,786
and just insert one document.

137
00:06:18,786 --> 00:06:22,919
And then, a document just looks like a JavaScript object

138
00:06:22,919 --> 00:06:25,757
with opening and closing curly braces.

139
00:06:25,757 --> 00:06:28,434
And between those curly braces,

140
00:06:28,434 --> 00:06:31,714
you can now have your key value peers.

141
00:06:31,714 --> 00:06:36,714
For example, for the restaurant, we can have a name.

142
00:06:36,994 --> 00:06:39,313
So we can add such a name field here.

143
00:06:39,313 --> 00:06:42,157
You can come up with any keys you want.

144
00:06:42,157 --> 00:06:45,270
And that name here could be a string created

145
00:06:45,270 --> 00:06:48,977
with double quotes, for example, "Munich Schnitzelhouse".

146
00:06:51,450 --> 00:06:54,380
And here, we already see our first data type,

147
00:06:54,380 --> 00:06:55,410
which you can use:

148
00:06:55,410 --> 00:06:59,527
a string created with quotes, just as in JavaScript.

149
00:06:59,527 --> 00:07:03,708
There also are the other data types you would expect,

150
00:07:03,708 --> 00:07:08,708
like numbers, or also dates, or also nested data types,

151
00:07:09,120 --> 00:07:11,611
like erase or nested objects.

152
00:07:11,611 --> 00:07:15,030
Because as I mentioned before in this section already,

153
00:07:15,030 --> 00:07:18,048
that's a specialty of NoSQL database systems,

154
00:07:18,048 --> 00:07:21,600
that you can have nested structured data

155
00:07:21,600 --> 00:07:25,010
instead of splitting your normalized flat data

156
00:07:25,010 --> 00:07:28,352
across multiple tables, as you would do it with SQL.

157
00:07:28,352 --> 00:07:33,051
So having nested data would be totally normal here.

158
00:07:33,051 --> 00:07:35,835
And indeed, we can already add our first piece

159
00:07:35,835 --> 00:07:37,952
of nested data here.

160
00:07:37,952 --> 00:07:42,641
Besides adding the name, we could add an address field here,

161
00:07:42,641 --> 00:07:45,888
because every restaurant should also have an address,

162
00:07:45,888 --> 00:07:50,324
and that now could indeed be some structured document.

163
00:07:50,324 --> 00:07:53,089
So another pair of curly braces,

164
00:07:53,089 --> 00:07:57,049
where we store the address for this specific restaurant

165
00:07:57,049 --> 00:07:59,426
as a sub-document, you could say,

166
00:07:59,426 --> 00:08:01,822
with more key value fields.

167
00:08:01,822 --> 00:08:05,838
For example, here, we could have the street,

168
00:08:05,838 --> 00:08:09,998
and that could be "Some Street 5".

169
00:08:09,998 --> 00:08:13,093
And then, separated by a comma,

170
00:08:13,093 --> 00:08:17,576
here we could also then have the street number,

171
00:08:17,576 --> 00:08:20,340
and that could be an actual number 25,

172
00:08:20,340 --> 00:08:24,020
or to support things like 23b,

173
00:08:24,020 --> 00:08:26,356
we could also make it a string again.

174
00:08:26,356 --> 00:08:30,590
Now here, I automatically have a line, a break,

175
00:08:30,590 --> 00:08:32,791
because I ran out of space in that first line,

176
00:08:32,791 --> 00:08:34,591
but it's still one command here.

177
00:08:34,591 --> 00:08:37,817
And with that, we could insert such a restaurant.

178
00:08:37,817 --> 00:08:40,909
Now, we'll come back to the question

179
00:08:40,909 --> 00:08:43,570
of whether it's a good idea to store the address

180
00:08:43,570 --> 00:08:46,441
as a nested object instead of a standalone document

181
00:08:46,441 --> 00:08:48,706
in a standalone collection,

182
00:08:48,706 --> 00:08:50,313
but for the moment that's the kind

183
00:08:50,313 --> 00:08:52,145
of restaurant I wanna store.

184
00:08:52,145 --> 00:08:56,990
And we can now hit Enter to send this command

185
00:08:56,990 --> 00:08:59,410
to the MongoDB server to which we're connected.

186
00:08:59,410 --> 00:09:00,805
And as a response,

187
00:09:00,805 --> 00:09:03,106
we get this kind of answer here,

188
00:09:03,106 --> 00:09:07,173
where we get the ID of the inserted document,

189
00:09:07,173 --> 00:09:11,258
because MongoDB will automatically create a unique ID

190
00:09:11,258 --> 00:09:14,397
for every document that's inserted.

191
00:09:14,397 --> 00:09:18,489
So that's kind of similar to SQL-based databases,

192
00:09:18,489 --> 00:09:20,850
where we also had unique IDs.

193
00:09:20,850 --> 00:09:22,050
We also have them here.

194
00:09:22,050 --> 00:09:23,580
They just look different.

195
00:09:23,580 --> 00:09:26,695
In the end, they are long, unique strings generated

196
00:09:26,695 --> 00:09:28,434
by the MongoDB server.

197
00:09:28,434 --> 00:09:31,063
And with that, our data is stored.

198
00:09:32,045 --> 00:09:35,350
Now let's add a second piece of data.

199
00:09:35,350 --> 00:09:39,310
And for this, I'll just quickly duplicate that command

200
00:09:39,310 --> 00:09:44,310
from before and switch the data, "Another Street".

201
00:09:45,602 --> 00:09:49,313
I just noticed I had a street number here in my first piece

202
00:09:49,313 --> 00:09:50,620
of data already.

203
00:09:50,620 --> 00:09:51,947
It doesn't matter too much.

204
00:09:51,947 --> 00:09:53,101
It's just an example.

205
00:09:53,101 --> 00:09:54,960
Of course, it should have been stored

206
00:09:54,960 --> 00:09:56,590
in this separate street number.

207
00:09:56,590 --> 00:09:59,080
So I've fixed this here in the second document,

208
00:09:59,080 --> 00:10:01,931
where I didn't add an extra number in the street here.

209
00:10:01,931 --> 00:10:06,931
And then, we can also go back here and change the name

210
00:10:07,170 --> 00:10:09,610
to "Burger House".

211
00:10:09,610 --> 00:10:12,132
I'm really creative here, as you can tell.

212
00:10:12,132 --> 00:10:14,713
And hit Enter to insert this as well.

