Coding

NodeJS / Express / MongoDB – Build a Shopping Cart – #3 MongoDB / Mongoose Setup

  • 00:00:00 hi welcome back so we finished with this
  • 00:00:04 nice-looking page here with our dummy
  • 00:00:06 thumbnails and what I want to do now is
  • 00:00:08 I will stick to dummy data for now or it
  • 00:00:12 with seated data to put it this way but
  • 00:00:14 I want to use the data base I want to
  • 00:00:16 introduce MongoDB and actually seat data
  • 00:00:20 there so that the data we are seeing on
  • 00:00:22 this page here on the index page
  • 00:00:24 actually comes from the database and
  • 00:00:27 it's not hard-coded into what the HTML
  • 00:00:30 code the first step to do this of course
  • 00:00:33 is to download and install MongoDB now I
  • 00:00:37 already get a video on that and you will
  • 00:00:39 find a link in the description
  • 00:00:40 nonetheless to quickly get you started
  • 00:00:43 the first step is to head over to
  • 00:00:45 MongoDB and there you simply want to
  • 00:00:51 press this download button here and then
  • 00:00:55 just choose your operating system and
  • 00:00:57 click this download link now if you were
  • 00:01:00 on Mac like I am then you will have a
  • 00:01:02 little package you simply have to
  • 00:01:03 extract and then just move to extract
  • 00:01:06 that folder somewhere on your machine if
  • 00:01:09 you're running Windows then you will
  • 00:01:11 find an installer for which you run and
  • 00:01:13 where you specify where this should be
  • 00:01:16 installed the next step is to open up a
  • 00:01:18 terminal or command line and then simply
  • 00:01:20 navigate into your MongoDB folder so
  • 00:01:23 where you moved that extract or
  • 00:01:25 installed folder once you navigate it
  • 00:01:27 into that folder navigate into the bin
  • 00:01:30 in the binary folder and then you run
  • 00:01:32 your MongoDB server by just running dot
  • 00:01:34 slash Mongo D on Mac or Linux or on
  • 00:01:37 Windows you may simply double click on
  • 00:01:38 it and Explorer or just type MongoDB in
  • 00:01:42 your command line and that fires up the
  • 00:01:45 MongoDB server which hosts the database
  • 00:01:47 and of course we need that server to
  • 00:01:49 work with it so that's the quick
  • 00:01:51 installation and in the description of
  • 00:01:53 the video you will also find a link to
  • 00:01:55 the official manual which detailee
  • 00:01:58 describes how to get mono DB installed
  • 00:02:01 and running on different operating
  • 00:02:02 systems so with that out of the way it's
  • 00:02:05 time to configure it and to use it in
  • 00:02:08 the shopping cart application in order
  • 00:02:12 to do this I'll open up in new terminal
  • 00:02:14 and the first thing I will do here is I
  • 00:02:16 will install Mongoose no Mongoose is
  • 00:02:20 basically a noche Express package which
  • 00:02:24 makes what most mom would be very simple
  • 00:02:27 now we could use the official MongoDB
  • 00:02:28 client and that would work fine too but
  • 00:02:31 Mongoose offers a little some nice
  • 00:02:34 extras basically it allows us to define
  • 00:02:37 schemas it has validation built in and
  • 00:02:40 you can work with the data in a very
  • 00:02:42 intuitive way and this is why I prefer
  • 00:02:45 it to the normal MongoDB driver to
  • 00:02:48 install Mongoose I just run NPM install
  • 00:02:50 – – safe to also create an entry deep
  • 00:02:53 package.json file and then Mongoose and
  • 00:02:55 this will go ahead and quickly install
  • 00:02:58 Mongoose on my machine and once this is
  • 00:03:02 installed I will head over to my apt J's
  • 00:03:05 file where the requests are handled so
  • 00:03:09 to say and I will important lose by
  • 00:03:12 simply typing requirements here and I
  • 00:03:16 will need to connect my app to the
  • 00:03:19 Mongoose server or to the MongoDB server
  • 00:03:21 I do this by using Mongoose and then the
  • 00:03:23 connect method and this connect method
  • 00:03:26 expects an input and the argument we
  • 00:03:29 have to pass here is the path of our
  • 00:03:32 server now my case this is of course
  • 00:03:34 localhost
  • 00:03:35 then port 2 7 o 17 and you can find out
  • 00:03:40 this port in the console where you start
  • 00:03:42 at the server and of course you have to
  • 00:03:44 keep this terminal window open disk this
  • 00:03:46 service running here wants to start up
  • 00:03:50 you will find this starting info where
  • 00:03:53 you will also see the port number and by
  • 00:03:55 default it's 27,000 17 so 2 0 17 that is
  • 00:04:01 why I entered this port here and then
  • 00:04:02 with a slash I define the database I
  • 00:04:04 want to use on this server because that
  • 00:04:08 is only the path to the server until now
  • 00:04:10 but the server might hold multiple
  • 00:04:12 databases of course and I want to use a
  • 00:04:15 database called shopping now you don't
  • 00:04:18 have to create the database it will be
  • 00:04:20 crypt automatically for you if it
  • 00:04:21 doesn't exist yet so all you do is
  • 00:04:23 basically just add slash shopping to use
  • 00:04:26 that
  • 00:04:27 now if this mongooses setup were yeah it
  • 00:04:30 setup and we are connected to MongoDB
  • 00:04:32 however we also need some way to
  • 00:04:37 actually create a seed data so the data
  • 00:04:40 which is in the database and right now I
  • 00:04:44 won't provide a way for the user to
  • 00:04:47 create new products or something like
  • 00:04:49 this so how could we do that we'll have
  • 00:04:52 a look at this in the next video see you
  • 00:04:53 there