1
00:00:00,450 --> 00:00:01,550
All right welcome back.

2
00:00:01,950 --> 00:00:04,980
As promised we're going to start writing some Mongo code.

3
00:00:04,980 --> 00:00:07,140
I have a list of commands here that we're going to go over.

4
00:00:07,140 --> 00:00:11,430
There's quite a few but it's really these bottom four that are the most important.

5
00:00:11,520 --> 00:00:13,060
But we need to start with some of these.

6
00:00:13,140 --> 00:00:16,070
So the first one is Man-God which we already ran.

7
00:00:16,090 --> 00:00:22,200
And the last video which starts our Mongo deman the mango process which is going to be running in the

8
00:00:22,200 --> 00:00:23,960
background for the rest of this course.

9
00:00:24,000 --> 00:00:26,610
We just have to have this running in order to use Mongo.

10
00:00:26,910 --> 00:00:34,050
So again we want this in a separate tab and then we'll have another tab where we'll type Mongo and this

11
00:00:34,050 --> 00:00:39,870
opens up the Mungo's show which as I mentioned in the last video is just like the javascript console

12
00:00:40,200 --> 00:00:43,330
in the sense that we use it to debug to test things out.

13
00:00:43,380 --> 00:00:48,280
Also we use it when we're learning like we are right now but we won't be using it for real to interact

14
00:00:48,290 --> 00:00:51,370
with their database and remove things and update things.

15
00:00:51,420 --> 00:00:53,650
We'll be doing that in some sort of file.

16
00:00:54,210 --> 00:01:01,050
So the first command that we can type is help and what help will do is just give us a list of some of

17
00:01:01,050 --> 00:01:07,470
the basic features of Mongo and you'll see that it shows things like show Debby's which is the very

18
00:01:07,470 --> 00:01:11,950
next thing that I want to talk about showed TBS will show database names.

19
00:01:12,210 --> 00:01:18,870
So if we try that right now show DVRs we see that there's admin and local and those are the two default

20
00:01:18,900 --> 00:01:21,140
databases that Mongo knows about.

21
00:01:21,510 --> 00:01:27,040
And the way that databases work is that we'll make a separate database for every app that we make.

22
00:01:27,090 --> 00:01:31,950
We don't have to but we're going to make a database for Yelp camp and we'll make another database for

23
00:01:32,280 --> 00:01:35,280
the dog app with the friends app or whatever else that we work on.

24
00:01:35,340 --> 00:01:40,230
We'll have a separate database so that things are self-contained and the way that we make another database

25
00:01:40,590 --> 00:01:43,340
is actually the same way that we use a database.

26
00:01:43,340 --> 00:01:49,300
It's this use command and then it looks like this use and then the name of the database.

27
00:01:49,410 --> 00:01:52,040
And if it exists then it will automatically use it.

28
00:01:52,200 --> 00:01:56,070
And if it doesn't exist it will make the database and then use it.

29
00:01:56,130 --> 00:02:03,570
So we'll make a new one here just called Demo use demo which doesn't exist but then it tells us switched

30
00:02:03,660 --> 00:02:10,380
to D-B demo and then I can also run show Digby's and we don't see demo yet because it's empty but as

31
00:02:10,380 --> 00:02:13,750
soon as we add some thing in we'll see a demo here as well.

32
00:02:13,860 --> 00:02:17,190
And the way that we add things in it is by creating collections.

33
00:02:17,190 --> 00:02:19,320
So for this demo we'll be using dogs.

34
00:02:19,410 --> 00:02:27,360
So we're going to have a collection of dogs and each dog can have things like a name or an age or breed

35
00:02:27,690 --> 00:02:29,440
but remember that this is Mungo.

36
00:02:29,550 --> 00:02:35,790
It's no sequel's non-relational So it's flexible so we can have a dog that only has a name and some

37
00:02:35,790 --> 00:02:37,140
dogs will have all of these.

38
00:02:37,230 --> 00:02:39,670
And then another dog might just be totally empty.

39
00:02:39,810 --> 00:02:43,970
And instead it might just have is dog to be true.

40
00:02:44,340 --> 00:02:49,980
So we're totally flexible here but we need to group things together into a collection and that collection

41
00:02:49,980 --> 00:02:50,970
will be dogs.

42
00:02:50,970 --> 00:02:55,950
And the way that we create a new collection just like with us we don't have to declare the collection

43
00:02:55,950 --> 00:03:00,890
ahead of time and then add to it we can just go ahead and insert immediately.

44
00:03:00,960 --> 00:03:05,730
So we're going to add a single dog into our database and that looks like this.

45
00:03:05,760 --> 00:03:13,420
D-B dot dogs dot insert and then we pass in the data that we want to insert.

46
00:03:13,440 --> 00:03:15,940
And before I do that let's analyze this.

47
00:03:15,960 --> 00:03:23,220
So D-B is referring to the D-B that we're on which is Demo dogs is the collection that doesn't exist

48
00:03:23,220 --> 00:03:29,100
yet but it will in just a moment and then insert is the command that will insert data into the dog's

49
00:03:29,100 --> 00:03:32,120
collection in this database which is dema.

50
00:03:32,460 --> 00:03:40,200
So we're going to insert in an object and this object will start very simple with name equal to resti

51
00:03:41,280 --> 00:03:47,090
And Breede equal to much just like that.

52
00:03:47,310 --> 00:03:54,330
And we can hit enter and they got a syntax error because I missed my quote so I'll go back and add that

53
00:03:54,330 --> 00:03:55,730
closing quotation mark.

54
00:03:56,040 --> 00:04:00,660
And now we get this message that says right result inserted one.

55
00:04:01,420 --> 00:04:09,030
And to prove that the dog's collection was created I can run show collections and that shows me I now

56
00:04:09,030 --> 00:04:16,980
have the dogs collection and if I want to now view all dogs in my database we need to use find and find

57
00:04:16,980 --> 00:04:18,020
works like this.

58
00:04:18,270 --> 00:04:21,370
The dog dogs find.

59
00:04:21,930 --> 00:04:26,090
And then if we don't pass anything it will just automatically return all the dogs.

60
00:04:26,130 --> 00:04:30,100
Everything in that collection and we get this one dog back.

61
00:04:30,270 --> 00:04:31,650
So it has breed.

62
00:04:31,760 --> 00:04:34,790
It has a name and then it has this crazy looking thing.

63
00:04:34,800 --> 00:04:41,090
Underscore ID which is an object id which is a bunch of numbers and letters hexadecimal here.

64
00:04:41,250 --> 00:04:46,430
And this is automatically assigned by Mongo and they're all unique.

65
00:04:46,470 --> 00:04:53,400
So if we add another one in we'll do right now we'll add another dog and this time we'll add in keep

66
00:04:53,400 --> 00:04:55,950
a breed of Mutt the ADD name.

67
00:04:55,950 --> 00:04:58,650
And to be Lucy and Hunter.

68
00:04:59,160 --> 00:05:06,600
And then if we do D-B Dogstar find again you'll see that we end up with two dogs now and each one has

69
00:05:06,600 --> 00:05:08,180
a unique object.

70
00:05:08,460 --> 00:05:11,200
So they look very similar and they are the same.

71
00:05:11,340 --> 00:05:18,690
Up until this very last digit or they change and that unique ID is useful later on we'll be using it

72
00:05:18,690 --> 00:05:25,050
to refer to specific dogs while not dogs but to specific items specific campgrounds or whatever the

73
00:05:25,050 --> 00:05:32,250
resources that we're working with so sometimes you only want to find a particular instance of a dog

74
00:05:32,260 --> 00:05:32,380
.

75
00:05:32,500 --> 00:05:38,700
So rather than finding all dogs or all of a particular resource we want to find everything that has

76
00:05:38,700 --> 00:05:45,420
breed of mutt where we want to find the dog with name equal to Rustie and we can do that by using D-B

77
00:05:45,690 --> 00:05:52,590
that dogs find and rather than just hitting Enter we pass in an object and let's say I want to find

78
00:05:52,590 --> 00:05:54,210
the dog where name is Rusty.

79
00:05:54,350 --> 00:06:02,910
I can do name Rusty and when I hit enter it will locate the dog or dogs that have name equal to resti

80
00:06:03,630 --> 00:06:07,850
and you can see I get this single dog with name equal to Rusty.

81
00:06:07,870 --> 00:06:15,090
Now let's add one more in here who is not a mutt and this one will be a poodle and the poodle's name

82
00:06:15,190 --> 00:06:18,950
will be Lulu.

83
00:06:19,800 --> 00:06:29,670
And now if I do a dogs up find everything I get three dogs and if I do find where breed is mutt

84
00:06:32,640 --> 00:06:40,290
just like that I get two dogs because we have two months we have Rusti and Lucy but I didn't get Lulu

85
00:06:40,380 --> 00:06:42,200
because she's a poodle.

86
00:06:43,120 --> 00:06:50,370
OK so we covered inserting into collections and retrieving or finding what's also called reading.

87
00:06:50,380 --> 00:06:59,250
There is an acronym crud which stands for create read update and destroy or delete and we've done the

88
00:06:59,250 --> 00:07:02,070
C and the our create and read.

89
00:07:02,070 --> 00:07:05,480
Now let's talk about how we can update particular dogs.

90
00:07:05,850 --> 00:07:11,070
Let's say that we figure out that Lulu is actually a labradoodle instead of a regular poodle.

91
00:07:11,100 --> 00:07:16,870
What we can do is update the value of breed on Lulu by using Mangu TBS update.

92
00:07:17,010 --> 00:07:22,940
So that looks like TB that dogs update and update takes two different things.

93
00:07:22,950 --> 00:07:31,420
The first one is something to select by and we want to select her name is Lulu or we could have selected

94
00:07:31,410 --> 00:07:36,640
where we're breed is poodle or where Id is this giant thing but I'll do select.

95
00:07:36,630 --> 00:07:44,370
Her name is Lulu and then we can go and update it so that we set Vrede to be labradoodle.

96
00:07:44,460 --> 00:07:51,690
I hope that's how you spell it and we can hit enter and if we now look at all the dogs or just Lulu

97
00:07:52,890 --> 00:07:58,630
you'll see that we updated Lulu except for one small issue which is that we totally overrode Lulu's

98
00:07:58,620 --> 00:07:59,460
name.

99
00:07:59,460 --> 00:08:06,250
What we did was we found all dogs her name is Lulu and then we updated her so that all she has is breed

100
00:08:06,270 --> 00:08:09,420
of Labradoodle to prevent that from happening.

101
00:08:09,610 --> 00:08:15,330
If we only wanted to update the breed while preserving the original name there is a slight alteration

102
00:08:15,330 --> 00:08:17,040
to our update that we can make.

103
00:08:17,220 --> 00:08:20,210
So this time let's just abandon Lulu.

104
00:08:20,250 --> 00:08:22,700
Unfortunately she's a lost cause.

105
00:08:23,160 --> 00:08:25,470
So let's suppose I wanted to change rusty.

106
00:08:25,650 --> 00:08:31,230
I want to change his name to be Tator but they also want to add in a new property which will call is

107
00:08:31,240 --> 00:08:33,580
cute which will be set to be true.

108
00:08:33,900 --> 00:08:42,420
So I need to run D-B dogs update and then I first need to give the update something to select by.

109
00:08:42,490 --> 00:08:48,640
So update dogs where name is Rusty and then the second thing is another object.

110
00:08:49,000 --> 00:08:52,020
And in that object we provide how we want it to be updated.

111
00:08:52,240 --> 00:08:54,450
And we don't want it to overwrite everything.

112
00:08:54,610 --> 00:09:01,450
So instead what we'll do is use dollar signs set and then set that to be another object.

113
00:09:01,750 --> 00:09:10,230
And in that object we're going to change the name to be Tator comma and we'll add in is cool or cute

114
00:09:10,250 --> 00:09:10,690
.

115
00:09:11,130 --> 00:09:14,170
Both of those are true but is cute to be true.

116
00:09:14,500 --> 00:09:18,660
OK so what we've done updated the dog's name is Rusty.

117
00:09:18,660 --> 00:09:20,290
There's only one right here.

118
00:09:20,700 --> 00:09:24,770
And then we use dollar signs set and provided an object to set.

119
00:09:24,930 --> 00:09:28,320
And what this will do is it will preserve the breed as mutt.

120
00:09:28,360 --> 00:09:32,540
It won't completely eliminate it like it did with Lulu's name here.

121
00:09:32,830 --> 00:09:38,390
And if I hit enter and now I look at all dogs or just the last dog DVD.

122
00:09:38,430 --> 00:09:39,910
Dogs find

123
00:09:42,850 --> 00:09:50,170
you can see here's the dog previously known as rusty or we have name is Tator breed is mutt and is cute

124
00:09:50,160 --> 00:09:50,220
.

125
00:09:50,220 --> 00:09:52,570
It's true.

126
00:09:52,600 --> 00:09:57,940
So that was updating which is the you in crud create read update.

127
00:09:57,960 --> 00:10:04,180
Now the last thing to talk about is destroying which in Mongo we use the remove command to accomplish

128
00:10:04,180 --> 00:10:04,460
.

129
00:10:04,750 --> 00:10:09,160
So we're going to begin by removing the dog previously known as Lulu.

130
00:10:09,420 --> 00:10:18,550
So we need to do a D-B that dogs remove and then we pass in the dogs that we want to be removed.

131
00:10:18,720 --> 00:10:25,200
And we want to remove where breed is a labradoodle just like that.

132
00:10:25,330 --> 00:10:29,740
If I hit enter once again I forgot my quote.

133
00:10:30,180 --> 00:10:38,180
Now if I hit enter and now we look at all dogs we can see we're down to just two dogs.

134
00:10:38,190 --> 00:10:39,650
This dog is now gone.

135
00:10:40,140 --> 00:10:47,100
Likewise if I tried to do the same thing but instead of leading where breed is Labradoodle I did where

136
00:10:47,110 --> 00:10:51,610
breed is mutt and they hit Enter now.

137
00:10:51,930 --> 00:10:53,470
Notice that it says right.

138
00:10:53,500 --> 00:10:54,180
Result.

139
00:10:54,270 --> 00:10:56,000
So the result of what we just did.

140
00:10:56,080 --> 00:10:57,460
Removed too.

141
00:10:57,780 --> 00:10:59,030
So let's check if that's true.

142
00:10:59,020 --> 00:11:08,430
TB That Dogstar find and it's true there are no dogs left so remove will by default remove everything

143
00:11:08,430 --> 00:11:15,500
that matches whatever you provided there is a way to specify how many dogs you want removed using that

144
00:11:15,510 --> 00:11:16,400
limit.

145
00:11:16,570 --> 00:11:22,030
Looks like this and you can pass in if we only wanted to remove one or two rather than all of them but

146
00:11:22,020 --> 00:11:25,510
by default it will remove everything that matches.

147
00:11:25,570 --> 00:11:27,760
All right so that's all we're going to do with Mongo for now.

148
00:11:27,960 --> 00:11:34,590
Let's just quickly wrap up so Man-God Mongo D starts the demon we need it running in order to use Mongo

149
00:11:34,600 --> 00:11:35,970
at all.

150
00:11:35,970 --> 00:11:42,180
Mongo opens a shell which is where we are right now and we can quit there with Control-C open it back

151
00:11:42,180 --> 00:11:43,580
up help.

152
00:11:43,600 --> 00:11:47,490
We won't use all that much but I want to show it to you so you know it's there.

153
00:11:47,500 --> 00:11:50,200
Show Digby's is important.

154
00:11:50,220 --> 00:11:55,070
It shows all the TBs that we have and you can see our demo D-B has now shown up.

155
00:11:55,600 --> 00:12:03,180
Then we have use so we can use one of those TBS like demo but we can also use the use command to create

156
00:12:03,180 --> 00:12:05,700
a new database and then use it.

157
00:12:05,700 --> 00:12:07,660
Then we have the four crud commands.

158
00:12:07,770 --> 00:12:14,580
So insert We used to create something fine we use to find it or to retrieve it update is used to update

159
00:12:14,590 --> 00:12:18,440
something or edit it and then remove is how we remove something.

160
00:12:19,220 --> 00:12:21,280
Ok so thats it in the next video.

161
00:12:21,270 --> 00:12:26,940
I'm going to show you how we can interact with Mangu TV from inside of a javascript file.
