- [Instructor] So now that we know what MongoDB is
and what we're going to use it for,
let's get it up and running on your computer.
Now, if you're using a Mac,
as I am in this video,
I highly recommend
that you install MongoDB using Homebrew.
And of course,
if you're using some other operating systems
such as windows,
I'm afraid I can't really help you too much at this point,
but feel free to follow
this link here to MongoDB's website,
that contains instructions
for installing MongoDB Community Edition
that is the free version of MongoDB
on all of the major operating systems.
So as far as installing MongoDB
on Mac using Homebrew,
if you don't already have Homebrew
installed in your computer,
you should go to Homebrew's website,
which is brew.sh
and they have instructions there
for installing Homebrew using the command line.
Basically, it's as simple
as just opening up a terminal,
copying this command that they have here
and pasting it in there
and hitting Enter.
And I'm not actually going to do this myself
since I already have it all set up
and I don't want to mess anything up here,
but you should feel free to run this
and that should set up Homebrew on your computer.
So once you've run that command
and installed Homebrew correctly,
it might take a little while,
but once you do that,
you can just type the command brew install mongodb
and hit Enter
and that should install MongoDB for you.
And again, I'm not going to do that
because I already have it installed,
but you should feel free
to run that on your own computer.
So after we've got MongoDB installed,
we just have to create
the default database directory
where our MongoDB files will live.
And to do this,
you just have to run the command sudo mkdir -p
and then /data,
/db
and hit Enter,
and you'll probably have to enter your password there
and that will set up
the default database directory for you.
And the next thing you have to do
is you need to make sure
that you have the permissions
to make changes in this directory.
So in order to do that,
you're just going to run the sudo
chown
- R
and then 'id
- un'
and then /data/db
and hit Enter,
and this might ask you
to enter your password again,
and once you've done that,
that's pretty much all it takes to set up MongoDB.
So again, if you're on another operating system,
your setup process is probably
going to be a lot different than this,
but anyway, just follow all the steps
in that tutorial on MongoDB's website
and you should be able
to come back here after that's done.
So now that we've got MongoDB installed and set up,
it's time to run it.
So to start the Mongo daemon,
you just have to enter
the command mongod into the terminal
and hit Enter,
and you should see it come up and running, okay?
And you're going to want to leave this terminal open
after you do that.
Now, granted I have mine running in the background
since this is another user on my computer
that I'm using to do my recordings,
so it's going to be a little different for mine,
but just make sure to leave that up
and running in another window,
and you'll probably want to open up
another terminal like that
to run this next command.
So the next command that we're going to run
is the command mongo,
and what this will do
is it will open up the Mongo shell,
which is where we can create
and modify Mongo databases on our machine.
Now, there are quite a few different commands
that we can use to interact with Mongo databases.
And I definitely don't expect you to know them already.
What I'm going to do here
is I'm going to show you all the commands
you'll need for this course
and if you're interested in learning more
about all the commands,
feel free to look at the docs
which you can access here.
So the first thing we're going to do here
is we're going to create a new database for our backend.
Now, in order to see what databases
you already have on MongoDB,
now granted you probably won't have any of these,
but in order to see it,
the command for that is show dbs
and that'll show you all the existing databases
that you have running locally.
So in order to create a database,
all we need to do is type the command use,
and then whatever name of the database we want to use.
What I'm going to call mine
is something like vue-db, okay?
And now, we're inside a database.
So the next thing we're going to do is,
since we're planning on replacing
the fake database JavaScript object
in our server with this database,
why don't we open up our backend code,
which is going to be inside of here
and what we're going to do
is we're just going to copy
and paste our products array
into the Mongo shell.
Now, before you do this,
I didn't actually realize this
when I was setting up the project
but what you actually want to do
is there are a few duplicate IDs
which will kind of give us
some weird console errors later on in the course.
So what you want to do is down here,
you're just going to want to change a few of these IDs
to something unique, okay?
So for example,
I'm going to change this one
to something like 112,
I'm going to change this one
to something like 223,
going to change this one
to something like 334, okay?
So anyway, once we've done that,
we're free to start copying
and pasting these product objects
into our Mongo database.
Now, your first instinct here
might be to just copy
and paste this entire array
and that was certainly my first instinct,
but for whatever reason,
I've found that the MongoDB shell
doesn't really like it when you do that.
And I'm not exactly sure why that is,
but when I try
and copy all of this data
into the Mongo shell,
it doesn't quite work correctly.
So what I'm going to do,
just to be safe
is copy and paste
all of these products one by one, okay.?
And the way we do that,
let's start off with the first one,
copy just that first Jason object.
And what we're going to do
is say dp.products,
which is a collection
that we want to insert
these objects into
and then .insertMany,
and this is going to be an array
and we'll paste our first item there, okay?
And then we'll do the second one,
make sure you do a comma there,
and then the third one.
And once we're done with that,
we're just going to close off the array like that,
we're going to add a closing parentheses like that,
and we're going to hit Enter
and we should see this thing printed out
that says that 12 new objects
were just inserted into our products' collection.
Okay, and now that we've actually inserted objects
into this collection,
which you can see by typing show collections, right?
And that shows us that we have
a products collection in our database now.
If we want to see these objects
that we just inserted,
all we have to do
is type dp.products.find
with empty curly braces like that
and hit Enter
and that'll print out
this kind of messy lump of text here, okay?
If we want to actually see it pretty printed,
that is neatly formatted,
we can add .pretty on the end of it,
and that will print it a little more nicely,
even though we have this big description here
which is sort of making it look more complicated
than it actually is.
So anyway, let's say that we want to find
a given product in this products collection.
Well, the way that we do that
is by adding in some criteria
in between the curly braces
in our find query.
So for example,
if we wanted to find a product by its ID,
we could say db.products.find,
and then inside the curly braces here,
we could say something like 123,
and we could say .pretty
and that would print out
our product with the ID 123.
And note also that MongoDB by default
adds this underscore ID property
to any of the records
that we insert into our collections.
And if we want to
we can use that as the unique key,
but since we already have these existing IDs,
let's just keep using those
for the rest of the course.
So that's what happens
if we say something like db.products.find,
but what happens when we know
that we want at most one product
to be returned from our query?
Well, in that case,
we can make the same kind of query,
except this time we use findOne instead.
And that will just return an object
instead of an array like find does.
And note here
that we don't want to use findOne.pretty
because that just doesn't work here,
it's already pretty printed.
So anyway, that's the product collection.
What are we going to do now
about the shopping cart data
that we still have on our server?
Well, here's what we're going to do for that.
We're going to create another collection called users,
which will store data
for each of the users of our application.
And for now we're only going to have one user
with an ID that we'll assign ourselves
and that user was just going to have
a property called cart items, okay?
So here's what this is all going to look like.
We're just going to say db.users.insertOne
and the ID of this user
is going to be something like 12345,
the cart items property of this user
is going to look like this,
it's going to be an array
that contains the IDs
of all the products
that a user has put into their shopping cart.
So in our case,
we'll just say something like 123, 234
and we'll maybe say 456.
Okay, and then we'll close that off
and should insert that user object
into the user's collection of our database.
So that's all we need for now.
The last thing to know
is that when you're done with the Mongo shell,
you can use Control + C to exit
and that's it.