Node.js Tutorials
Lesson 2 of 14 · Node.js Part 2 – Install MongoDB in Windows · Information Technology
Node.js Part 2 – Install MongoDB in Windows
in this video we're going to install MongoDB onto a Windows machine and we're gonna be using this within our node application and what more go DB is it's
it's a no SQL database which is quite different from a traditional relational database like MySQL or Postgres it doesn't have tables it uses what's called collections which are kind of
equivalent to a table in in a regular SQL database but you don't have to hard code the schema it's very fast very scalable because of that because you don't need to hard code things I like
you do with with traditional databases I would definitely suggest coming to MongoDB org and looking through the documentation there's a lot more there than I can teach you so I mean this here
is a good a good definition of what MongoDB is so it's a document database and like I said collections are sort of equivalent to tables and documents are kind of equivalent equivalent to a
column alright so they're basically JSON objects they have name value pairs all right MongoDB is very high-performance embedding makes reads and writes very
fast high availability availability and of course easy scalability alright there's what's called sharding which distributes collection data across machines which we're not really going to
get into that this is kind of a beginners tutorial to MongoDB and the data mondal like i said it holds a set of collections a collection holds a set of documents all right and like I
said a document is a set of key value pairs so enough on what it is let's get it installed so I'm on Windows so I'm gonna go to downloads and click Windows alright and it's gonna give us this zip
file which I already have so I'm not gonna download it again I'm gonna go to that zip file and if we open that up you'll have a folder like this and you click in that and then you'll see this
bin folder with some other readme files and and such now there's no windows installer like there is for node so these are the actual files we need to use so I'm going to go to my C Drive and
you can put these wherever you want I'm just gonna put them in the root of my C Drive in a folder called MongoDB so I'm going to open up that folder and we're gonna drag this these files into it
now before we do anything we need to create a couple folders for MongoDB the first one is going to be our data folder so we want to create a folder called data and inside there we want to create
another folder called DB and this will hold all of our database information a la database data next one we want to create is the log file and that's just where all the logs will go for MongoDB
and these are not the default locations the default for Windows I believe would be right in the C Drive but I want them in the MongoDB folder so we just need to specify that when we when we access the
Mongo D program which is in the bin folder so Mongo D and Mongo are the two that we'll be using so let's go ahead and open up a command line inside of the bin folder we can
right-click and do get bash you can also use their standard Windows command line so what we want to do here is we want to say Mongo D and as a few things we need to do here
we need to manually define where our data and log folders are so the first thing we need to do is say directory per DB 1 / DB and we want to say DB path and then the path to our DB data folder now
there's something really important that you need to know if you're using this get bash program if you're using Windows command line the standard command line that comes with
Windows you want to use back slashes all right so you would say Mongo DB boards backslash whatever if you're using this git bash program you want to use forward slashes this is the UNIX style path
when using this git bash program we're pretty much using UNIX syntax so it's very important that you use forward slash in this program and because that really stumped me for a whole day I
couldn't understand why it wasn't connecting success successfully and it was because they couldn't find the folders because I used back slashes alright so now that we get that out of
the way we can say MongoDB let's see there I go slash data / – be alright and that is this folder here or this folder so we want to do the same thing with the log path
so we'll say log path see that's MongoDB and log and we want to define a file so we're gonna call this MongoDB dot log and you can name this whatever you want okay it doesn't
even have to have a log extension you could use anything alright so next thing we want to say is log a pen so that we don't overwrite our logs we just append them and then we're gonna say rest so
that we can use rest services and that's that's really it if we click that all output going to our log file that's good let's make sure that it was created it was all right so our logs will go here
so basically it's waiting for us to connect ok so let's open up another command line we'll open it in the bin folder and we want to type in Mongo and now
we're connected okay we can now say show DBS we should get local and we're connected we couldn't we can start creating collections we can add data we can do all that but before we do any of
that right now how we have it we have to manually start this when we want to use Mongo all right so if we get out of this with ctrl C same here and then we say Mongo again it's not going to connect we
get a connect failed so what we need to do is install Mongo DB as a Windows service and that's very easy to do if we just go and click the up arrow and we want to run this again except we just
want to tack on install to the end of it so what that did is created a service for us it says service can be started from the command line using net start MongoDB so
let's go ahead and do that all right so it started successfully and you can see it's not wait it's not waiting we can go ahead and keep typing commands we don't even need this open
anymore and to verify that it was added we can go to the Windows Start menu and say services and then click on this services right here with the gear icon and scroll down and you can see our
services right here at MongoDB it started it's gonna start automatically so that way we don't need to do anything to start MongoDB we can just connect so we're gonna clear this out and type in
Mongo which will connect to our MongoDB now we want to create a database called kbase because by default we're connecting to a database called test and we can do that just by saying use kbase
and it's as easy as that to create a database everything is just simplified in MongoDB if you're used to using a relational SQL database then this tool just shall blow
your mind how easy it is to do things alright so we switched to kbase now what we want to do is we can say show collections remember collections I'll kind of like tables and all we have is
just system dot indexes which we don't pay any attention to right now so to create a collection we can say DB create collection and we'll create a collection called
articles all right so if we get that okay one that's good now if we say show collections we can see that we have an articles collection now we can perform queries on this collection we can insert
update read and delete entries or articles so let's create let's um let's add something to it okay so we'll say DB dot articles dot insert all right so we
can perform an insert query like this all right so we want to say tied up look title we'll just say test type actually we'll see foo all right so that's the title put a
comma and we'll create another field which will be body and we can create these on the fly we don't have to we don't have to define them in any way we can just create them as we go so body
I'll just say this is a test body unexpected token so DB articles dot insert I'm sorry we have to wrap this in in curly braces it's basically JSON all right so if we
get nothing back that's good so what we can do now is say DB dot articles dot find and that'll show us everything that's that was entered which is our full article and we can also format this
a little better if we say find and then do dot pretty that gives it to us nice and neat so let's let's add another article and of course this one would be called Bar say
this is another test body so we'll do fine pretty and it gives us our two entries now we can find specific articles by a specific field if we want to will say find actually we're going to
say find one and then inside here we'll say title we want to find the article with the title of fou I'm sorry we have time I'm gonna line forgetting to wrap these
in curly braces alright so then it prints out that one entry which as Phu is the title alright so we've done a create and read so let's go to update so we can do this with DVD articles dot
update alright so inside here the first parameter is going to be what which which one we want to change alright so we want to change title whoever has a title of fou alright and the next
parameter we want to put a comma and make sure you include these curly braces the next thing we want to do is set it we want to set so let's rename it let's rename the title
so we'll just do our curly braces and we'll say title title is going to equal fubar alright and then we have one last parameter so we want to do a comma and inside our curly braces we're gonna say
upset is true and what this will do is if if it can't find something with the title of fubar then it's gonna create a new one all right so that's what this what this does here so let's run that
you got nothing back which is good and we'll call fine pretty and let's see title fubar id title bar alright so that doesn't look very pretty
huh all right so it looks like the body was also removed when we did death so let's go back here and put a comma here and we'll say body
alright so we just added the body as well okay we have to update that now we don't have to follow a schema here our articles have a title and a body and an ID which is an object ID but we can add
let's add a new article probably easier just to type but we'll say insert will create another article with the title of foo and then after the body let's put a comma and let's say category and our
category will be web design and let's show find pretty and you can see that this article now has a title body and a category so you can see how much freedom this gives you you don't have to hard
code a title body category if you just want to have a category two one entry all right so that's what's great about MongoDB and I'm not going to be a MongoDB fanboy and say that you should
always use this because it's definitely situations and different programs where you would need it would be a much better choice to use something like MySQL so this isn't an end-all solution alright
so now that we've done create read and update of course we need to do a delete or remove so we'll say DB dot sadee be articles dot remove and we'll
say we want to remove we're title is equal to bar alright and now the entry with the title of bar is now gone alright so that should do it to recap we downloaded MongoDB we
installed it and defined our data in log directories and then we installed it as a service and we went in and created a couple entries we did some basic crud operations and that's that so we won't
be doing much more with MongoDB in the command line we're gonna use the third party module mongoose which which we installed in our node app and we'll be dealing with the database through there
I don't know if we'll do that in the next video because I kind of want to get into routes a little bit and just create some new routes for our application but eventually we'll be dealing with the
database through Express and through Mongoose
Ill show you how to install and configure MongoDB on to a Windows machine.