Coding

Node.js + Express – Tutorial – Insert and Get Data with MongoDB

  • 00:00:01 hi welcome back time to actually use
  • 00:00:04 MongoDB in our Express application to
  • 00:00:08 get started up first
  • 00:00:09 also as waste create a new folder to a
  • 00:00:14 call Mongo DB and I will copy the
  • 00:00:22 structure from section 7 lecture 7 get
  • 00:00:26 posts because I don't need validation
  • 00:00:28 session management in this video here so
  • 00:00:31 that's fine and in here the sort of
  • 00:00:37 package of JSON file looks like any in
  • 00:00:40 here I first want to install a new
  • 00:00:42 package the MongoDB package of course
  • 00:00:46 so let me CD enter this new folder and
  • 00:00:50 then I run MP install – – save MongoDB
  • 00:00:57 and this will install the official
  • 00:01:00 MongoDB Express extension or the general
  • 00:01:04 noches MongoDB driver
  • 00:01:06 so whatever can use MongoDB so this is
  • 00:01:09 done as you can see it updated it here
  • 00:01:11 and now we don't have to set up anything
  • 00:01:16 we can use it straight away and first is
  • 00:01:20 I want to create a pleasant front-end
  • 00:01:23 and I prepared something here as it is
  • 00:01:25 really just some HTML and styles so let
  • 00:01:28 me copy that into my project so first
  • 00:01:33 styling here
  • 00:01:39 and then I need to test you by the way
  • 00:01:43 and then hear some HTML all this will do
  • 00:01:48 if I run this server here oh and I have
  • 00:01:54 to stop the other server first let me
  • 00:01:57 run the server if I reload this is what
  • 00:02:01 I created which will allow us allow us
  • 00:02:03 to do the four crud operations insert
  • 00:02:06 data getting data update and the leading
  • 00:02:07 data so this is just lay out a few and
  • 00:02:11 of course you'll find the code in a
  • 00:02:14 github repository as always with that
  • 00:02:20 out of the way let's jump right into our
  • 00:02:21 index dot J's file on the routes
  • 00:02:23 directory get rid of all the other
  • 00:02:26 routes you for the moment and then I
  • 00:02:29 need to add to imports here at the top
  • 00:02:32 the first is called Mongo and this
  • 00:02:39 requires of course MongoDB next is
  • 00:02:43 called assert requires assert and this
  • 00:02:48 is a built-in packaging nodejs normally
  • 00:02:52 used for for testing it allows us to
  • 00:02:54 compare values and so on and we use this
  • 00:02:57 when we connect to our MongoDB database
  • 00:03:00 or when we do some operations here there
  • 00:03:02 to check if everything went right the
  • 00:03:06 next things I will set up the URL which
  • 00:03:08 points to the database this will be a
  • 00:03:11 string and now this is of course not not
  • 00:03:15 optional and not arbitrarily it does
  • 00:03:18 Mongo DB da a colon slash slash and then
  • 00:03:24 localhost as is running on our local
  • 00:03:27 machine here we're doing all the
  • 00:03:29 development here now local machinist you
  • 00:03:31 probably noticed and that it's by
  • 00:03:34 default port 2 7 OS 17 but you may check
  • 00:03:38 this here in the terminal where your
  • 00:03:41 server is running when it started up the
  • 00:03:46 very first message here said the port or
  • 00:03:49 specify the port on which is running to
  • 00:03:52 seven
  • 00:03:53 not even my case here and this is the
  • 00:03:55 default so this will connect to this
  • 00:03:58 MongoDB then we add another part to this
  • 00:04:01 URL in this case slash test which
  • 00:04:05 defines database you want to use in our
  • 00:04:08 running MongoDB server so it does case
  • 00:04:12 we'll use the built-in test database
  • 00:04:13 which you already have now with this out
  • 00:04:16 of the way I will also change this to
  • 00:04:19 just render my index page here and then
  • 00:04:22 I will set up some new routes which I
  • 00:04:24 will need I want to have a route to get
  • 00:04:28 the data let's call this get data
  • 00:04:40 and I won't do anything there as of now
  • 00:04:42 I will this I will add the step by step
  • 00:04:45 then I will have a post route insert by
  • 00:04:54 the way here I probably should add this
  • 00:04:57 that's more correct this way and this
  • 00:05:02 insert route is post or I'll copy this
  • 00:05:04 two times because I'll also have update
  • 00:05:11 post route and a delete post route let's
  • 00:05:16 start with inserting items in my own
  • 00:05:19 certain route from my view from the form
  • 00:05:25 I have here for inserting items I will
  • 00:05:27 get a title content and offer so in here
  • 00:05:34 I will extract these parameters and
  • 00:05:37 create a new item which I want to insert
  • 00:05:40 so this will be a chava script object
  • 00:05:42 which of course will as I just said have
  • 00:05:45 a title I'll find us in the body of my
  • 00:05:48 post request and there it will have well
  • 00:05:51 property of title because that's the
  • 00:05:53 name of the foreign field that I will
  • 00:05:56 have content which is request body
  • 00:05:59 content and then I will have my offer
  • 00:06:02 request body offer and I have a typo
  • 00:06:08 content so this will be the item I want
  • 00:06:12 to insert and after inserting I want to
  • 00:06:16 redirect to let's say my route route my
  • 00:06:22 yeah this is in extra audio
  • 00:06:25 now this interesting part actually
  • 00:06:28 inserting the item so to actually insert
  • 00:06:31 something I will call Mongo connect
  • 00:06:36 connect to the database I specify the
  • 00:06:40 URL to connect you and then I call back
  • 00:06:42 once the connection has been well
  • 00:06:45 established or possibly declined a
  • 00:06:48 denied so here I will have an error
  • 00:06:52 we do have one or the database if we
  • 00:06:55 were successful I will then use assert
  • 00:06:58 tui to check if well if we do have an
  • 00:07:03 error or to check if we don't have an
  • 00:07:05 error to be precise and if we don't have
  • 00:07:08 one it's fine we will continue I will
  • 00:07:11 then access my database type it your
  • 00:07:13 fast here access my database which I
  • 00:07:15 went back to which I connected use the
  • 00:07:18 collection method on this and here I
  • 00:07:22 will specify the name of the collection
  • 00:07:25 into which I want to insert my data now
  • 00:07:28 the collection is not the same as
  • 00:07:30 database databases test but one database
  • 00:07:32 might have multiple collections like a
  • 00:07:35 sequel database might have multiple
  • 00:07:36 tables so I may enter whatever name I
  • 00:07:40 like here I will just call this user
  • 00:07:43 data this will be the name of my
  • 00:07:45 collection and here I will use the
  • 00:07:47 insert one method to insert exactly one
  • 00:07:50 entry this entry I insert will be my
  • 00:07:54 item here of course this function has an
  • 00:07:56 hour argument which will be the callback
  • 00:07:58 once this insertion has been completed
  • 00:08:01 or again an error has occurred so I get
  • 00:08:04 the error or a result I will again
  • 00:08:07 assert if we don't have an error so that
  • 00:08:12 we only continue if we do not have an
  • 00:08:14 error and then I just want you well lock
  • 00:08:18 to the console that we inserted item and
  • 00:08:22 then I will close the database
  • 00:08:24 connection by right typing we're using
  • 00:08:27 DB closed now we could test this but as
  • 00:08:30 of now we would not be able to see if it
  • 00:08:32 were it because we haven't specified to
  • 00:08:34 get data functionality so let's do this
  • 00:08:37 next to get data
  • 00:08:39 I will also open a connection to my
  • 00:08:43 database here so I will use Mongo
  • 00:08:46 connect to the URL and have the very
  • 00:08:50 same function or call bag i eated have
  • 00:08:53 whenever as inserting items of course
  • 00:08:55 because this is just a connection thing
  • 00:08:58 here I will then copy that assert test
  • 00:09:02 here or this check if we do have an
  • 00:09:04 error
  • 00:09:05 and if we don't have an error I will get
  • 00:09:09 the items now getting the items a little
  • 00:09:12 bit different you inserting one because
  • 00:09:14 here obviously were just well pushing
  • 00:09:17 something into our database but we don't
  • 00:09:20 have to store any result value here I
  • 00:09:23 will get something back so we'll create
  • 00:09:25 a new variable which we'll call cursor
  • 00:09:29 because it will be a cursor pointing to
  • 00:09:32 well the data we're getting back and I
  • 00:09:37 will use DB collection again and use of
  • 00:09:40 course the same collection as below when
  • 00:09:42 we insert data but now I will call the
  • 00:09:45 find method which will get me all the
  • 00:09:49 entries in this collection and all these
  • 00:09:51 entries where pointers to these entries
  • 00:09:54 are stored in this career variable now
  • 00:09:58 in order to get the actual data I will
  • 00:10:01 run through all the entries I've found
  • 00:10:04 by using cursor for each and then a
  • 00:10:10 function which should be executed on
  • 00:10:13 each of these items so this function
  • 00:10:17 will be one that has first F argument
  • 00:10:22 which will be the current document and
  • 00:10:25 possible error again let's assert that
  • 00:10:31 error that error is now and then I want
  • 00:10:36 to store the actual value in some array
  • 00:10:40 which it will once I've got all the
  • 00:10:42 values returned to our view so in order
  • 00:10:46 to do this I will create a new variable
  • 00:10:48 here at the top which will call me salt
  • 00:10:49 array which is an empty area to begin
  • 00:10:52 and here I will then push all the items
  • 00:10:56 I'm extracting here so I will push dock
  • 00:11:00 which is the current item where we're in
  • 00:11:03 we're at the current iteration so we're
  • 00:11:06 running through all the items our career
  • 00:11:07 points too and then we're getting each
  • 00:11:11 item in each iteration which will be
  • 00:11:12 stored in this dark variable here and we
  • 00:11:15 just push this dock our very
  • 00:11:17 onto a results array so this will be the
  • 00:11:22 items through which we run and then I
  • 00:11:25 will have a number call back here in my
  • 00:11:27 foreach loop because this will be
  • 00:11:32 executed on each iteration but after all
  • 00:11:37 these iterations I will have a callback
  • 00:11:38 which is executed once
  • 00:11:41 well once I have looped through all the
  • 00:11:43 items I'm pointing to you so in this
  • 00:11:46 case I then want to close my database
  • 00:11:48 because I'm finished I'm done I
  • 00:11:50 retrieved all the items I wanted and I
  • 00:11:53 will render my index page and it doesn't
  • 00:11:59 work they do this in this callback
  • 00:12:00 because if I would do this outside of
  • 00:12:03 this whole block here it would get
  • 00:12:06 executed immediately because this plot
  • 00:12:08 runs asynchronously
  • 00:12:10 this means we started and then we
  • 00:12:12 immediately continue with the next
  • 00:12:13 command we might have here so if we
  • 00:12:16 render our you here it would not
  • 00:12:19 actually have data because this might
  • 00:12:21 not have yet finished so we couldn't
  • 00:12:24 output the data therefore we're
  • 00:12:26 rendering the view once we're sure that
  • 00:12:29 we're done fetching all the data and
  • 00:12:31 here I will render the index page and I
  • 00:12:36 will pass the items the result array I
  • 00:12:43 created and items is a variable which is
  • 00:12:47 used in the view here to output them so
  • 00:12:51 that's been a lot of coding let me
  • 00:12:53 restart the server and I get an error
  • 00:12:56 here right this should be assert equal
  • 00:12:59 of course so let me restart the server
  • 00:13:02 reload this page load data won't get me
  • 00:13:07 anything
  • 00:13:08 insert some data like this is a test
  • 00:13:13 test the content by me and now let's
  • 00:13:20 load data and I do have a tiny mistake
  • 00:13:24 here this should be error so now start
  • 00:13:29 and let's try this this is a test test
  • 00:13:34 content max insert load data and also
  • 00:13:40 got some other data this is because
  • 00:13:41 behind the scenes I was looking for this
  • 00:13:45 error here and I well tested it of
  • 00:13:49 course and therefore I have our data
  • 00:13:52 here but this is the data in our
  • 00:13:53 database and this is how we insert data
  • 00:13:56 now regarding updating and deleting
  • 00:13:58 we'll have a look at this in the next
  • 00:14:00 video see you there bye