All right welcome back.
As promised we're going to start writing some Mongo code.
I have a list of commands here that we're going to go over.
There's quite a few but it's really these bottom four that are the most important.
But we need to start with some of these.
So the first one is Man-God which we already ran.
And the last video which starts our Mongo deman the mango process which is going to be running in the
background for the rest of this course.
We just have to have this running in order to use Mongo.
So again we want this in a separate tab and then we'll have another tab where we'll type Mongo and this
opens up the Mungo's show which as I mentioned in the last video is just like the javascript console
in the sense that we use it to debug to test things out.
Also we use it when we're learning like we are right now but we won't be using it for real to interact
with their database and remove things and update things.
We'll be doing that in some sort of file.
So the first command that we can type is help and what help will do is just give us a list of some of
the basic features of Mongo and you'll see that it shows things like show Debby's which is the very
next thing that I want to talk about showed TBS will show database names.
So if we try that right now show DVRs we see that there's admin and local and those are the two default
databases that Mongo knows about.
And the way that databases work is that we'll make a separate database for every app that we make.
We don't have to but we're going to make a database for Yelp camp and we'll make another database for
the dog app with the friends app or whatever else that we work on.
We'll have a separate database so that things are self-contained and the way that we make another database
is actually the same way that we use a database.
It's this use command and then it looks like this use and then the name of the database.
And if it exists then it will automatically use it.
And if it doesn't exist it will make the database and then use it.
So we'll make a new one here just called Demo use demo which doesn't exist but then it tells us switched
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
soon as we add some thing in we'll see a demo here as well.
And the way that we add things in it is by creating collections.
So for this demo we'll be using dogs.
So we're going to have a collection of dogs and each dog can have things like a name or an age or breed
but remember that this is Mungo.
It's no sequel's non-relational So it's flexible so we can have a dog that only has a name and some
dogs will have all of these.
And then another dog might just be totally empty.
And instead it might just have is dog to be true.
So we're totally flexible here but we need to group things together into a collection and that collection
will be dogs.
And the way that we create a new collection just like with us we don't have to declare the collection
ahead of time and then add to it we can just go ahead and insert immediately.
So we're going to add a single dog into our database and that looks like this.
D-B dot dogs dot insert and then we pass in the data that we want to insert.
And before I do that let's analyze this.
So D-B is referring to the D-B that we're on which is Demo dogs is the collection that doesn't exist
yet but it will in just a moment and then insert is the command that will insert data into the dog's
collection in this database which is dema.
So we're going to insert in an object and this object will start very simple with name equal to resti
And Breede equal to much just like that.
And we can hit enter and they got a syntax error because I missed my quote so I'll go back and add that
closing quotation mark.
And now we get this message that says right result inserted one.
And to prove that the dog's collection was created I can run show collections and that shows me I now
have the dogs collection and if I want to now view all dogs in my database we need to use find and find
works like this.
The dog dogs find.
And then if we don't pass anything it will just automatically return all the dogs.
Everything in that collection and we get this one dog back.
So it has breed.
It has a name and then it has this crazy looking thing.
Underscore ID which is an object id which is a bunch of numbers and letters hexadecimal here.
And this is automatically assigned by Mongo and they're all unique.
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
a breed of Mutt the ADD name.
And to be Lucy and Hunter.
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
a unique object.
So they look very similar and they are the same.
Up until this very last digit or they change and that unique ID is useful later on we'll be using it
to refer to specific dogs while not dogs but to specific items specific campgrounds or whatever the
resources that we're working with so sometimes you only want to find a particular instance of a dog
.
So rather than finding all dogs or all of a particular resource we want to find everything that has
breed of mutt where we want to find the dog with name equal to Rustie and we can do that by using D-B
that dogs find and rather than just hitting Enter we pass in an object and let's say I want to find
the dog where name is Rusty.
I can do name Rusty and when I hit enter it will locate the dog or dogs that have name equal to resti
and you can see I get this single dog with name equal to Rusty.
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
will be Lulu.
And now if I do a dogs up find everything I get three dogs and if I do find where breed is mutt
just like that I get two dogs because we have two months we have Rusti and Lucy but I didn't get Lulu
because she's a poodle.
OK so we covered inserting into collections and retrieving or finding what's also called reading.
There is an acronym crud which stands for create read update and destroy or delete and we've done the
C and the our create and read.
Now let's talk about how we can update particular dogs.
Let's say that we figure out that Lulu is actually a labradoodle instead of a regular poodle.
What we can do is update the value of breed on Lulu by using Mangu TBS update.
So that looks like TB that dogs update and update takes two different things.
The first one is something to select by and we want to select her name is Lulu or we could have selected
where we're breed is poodle or where Id is this giant thing but I'll do select.
Her name is Lulu and then we can go and update it so that we set Vrede to be labradoodle.
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
you'll see that we updated Lulu except for one small issue which is that we totally overrode Lulu's
name.
What we did was we found all dogs her name is Lulu and then we updated her so that all she has is breed
of Labradoodle to prevent that from happening.
If we only wanted to update the breed while preserving the original name there is a slight alteration
to our update that we can make.
So this time let's just abandon Lulu.
Unfortunately she's a lost cause.
So let's suppose I wanted to change rusty.
I want to change his name to be Tator but they also want to add in a new property which will call is
cute which will be set to be true.
So I need to run D-B dogs update and then I first need to give the update something to select by.
So update dogs where name is Rusty and then the second thing is another object.
And in that object we provide how we want it to be updated.
And we don't want it to overwrite everything.
So instead what we'll do is use dollar signs set and then set that to be another object.
And in that object we're going to change the name to be Tator comma and we'll add in is cool or cute
.
Both of those are true but is cute to be true.
OK so what we've done updated the dog's name is Rusty.
There's only one right here.
And then we use dollar signs set and provided an object to set.
And what this will do is it will preserve the breed as mutt.
It won't completely eliminate it like it did with Lulu's name here.
And if I hit enter and now I look at all dogs or just the last dog DVD.
Dogs find
you can see here's the dog previously known as rusty or we have name is Tator breed is mutt and is cute
.
It's true.
So that was updating which is the you in crud create read update.
Now the last thing to talk about is destroying which in Mongo we use the remove command to accomplish
.
So we're going to begin by removing the dog previously known as Lulu.
So we need to do a D-B that dogs remove and then we pass in the dogs that we want to be removed.
And we want to remove where breed is a labradoodle just like that.
If I hit enter once again I forgot my quote.
Now if I hit enter and now we look at all dogs we can see we're down to just two dogs.
This dog is now gone.
Likewise if I tried to do the same thing but instead of leading where breed is Labradoodle I did where
breed is mutt and they hit Enter now.
Notice that it says right.
Result.
So the result of what we just did.
Removed too.
So let's check if that's true.
TB That Dogstar find and it's true there are no dogs left so remove will by default remove everything
that matches whatever you provided there is a way to specify how many dogs you want removed using that
limit.
Looks like this and you can pass in if we only wanted to remove one or two rather than all of them but
by default it will remove everything that matches.
All right so that's all we're going to do with Mongo for now.
Let's just quickly wrap up so Man-God Mongo D starts the demon we need it running in order to use Mongo
at all.
Mongo opens a shell which is where we are right now and we can quit there with Control-C open it back
up help.
We won't use all that much but I want to show it to you so you know it's there.
Show Digby's is important.
It shows all the TBs that we have and you can see our demo D-B has now shown up.
Then we have use so we can use one of those TBS like demo but we can also use the use command to create
a new database and then use it.
Then we have the four crud commands.
So insert We used to create something fine we use to find it or to retrieve it update is used to update
something or edit it and then remove is how we remove something.
Ok so thats it in the next video.
I'm going to show you how we can interact with Mangu TV from inside of a javascript file.