Coding

Node.js + Express – Tutorial – MongoDB + Monk

  • 00:00:01 hi everyone welcome back in the last
  • 00:00:03 videos we had a look at well interacting
  • 00:00:06 over longer with database and we used
  • 00:00:09 two native mondo DB driver as supplied
  • 00:00:13 by the MongoDB team and while this
  • 00:00:16 driver isn't too bad we used two point X
  • 00:00:20 version of it and here you can see the
  • 00:00:23 code we wrote and well it isn't too bad
  • 00:00:26 these are really are lots of lines of
  • 00:00:29 codes for example for simply retrieving
  • 00:00:32 all our data here and you may do it this
  • 00:00:37 way it is necessarily wrong but in this
  • 00:00:40 video I at least want to show you an
  • 00:00:42 alternative which cuts down these lines
  • 00:00:46 of codes which is sir you might say an
  • 00:00:49 additional layer using the MongoDB
  • 00:00:51 driver we used in the last parts but
  • 00:00:55 then abstracting it a little bit away or
  • 00:00:58 make him a little easier providing us an
  • 00:01:01 easier access and doing all the stuff we
  • 00:01:03 wrote here by hand behind the scenes so
  • 00:01:06 this is something we'll have a look at
  • 00:01:09 in this video and the name of this
  • 00:01:11 package which test is for us is monkey
  • 00:01:14 now here's an important thing in the
  • 00:01:18 last videos we used to MongoDB driver
  • 00:01:20 version 2.0 this driver already isn't
  • 00:01:25 too bad as you saw in the videos monke
  • 00:01:29 requires MongoDB one point for the
  • 00:01:32 driver the package one point for
  • 00:01:36 something because it is older version
  • 00:01:40 all the functionality we need is
  • 00:01:41 included but it well it has less of this
  • 00:01:45 easy access we used in the last videos
  • 00:01:48 so therefore you really have to compare
  • 00:01:52 the axis of the last videos with what
  • 00:01:54 we're doing now with monk okay now
  • 00:01:57 talking let's get started so what I will
  • 00:02:00 do is I will take our MongoDB project
  • 00:02:03 here and just well copy tips and give it
  • 00:02:08 a new name of or reciprocal it monkey
  • 00:02:10 because this names the package we'll be
  • 00:02:12 using
  • 00:02:14 if two seconds to finish yes or to add
  • 00:02:18 it to get thank you so here I am now the
  • 00:02:21 first thing I'll do is in the package
  • 00:02:23 I'll change the version of MongoDB and I
  • 00:02:28 will change it to version one point four
  • 00:02:31 point one the next thing I'll do is I'll
  • 00:02:34 go into my terminal here make sure that
  • 00:02:36 I'm in this newly created folder and I
  • 00:02:38 will run npm install to install this new
  • 00:02:40 one would it be worship Wednesdays is
  • 00:02:43 finished I will npm install with saved
  • 00:02:46 flag mod and it should finish with it
  • 00:02:52 without any errors so no monk is
  • 00:02:55 installed as you can see the package so
  • 00:02:57 chase updated and now we may use it now
  • 00:03:00 in my index 30s file I will first get
  • 00:03:04 rid of all our MongoDB code here because
  • 00:03:13 of course we will the brightest from
  • 00:03:15 scratch using monk now using long is
  • 00:03:19 really easy completed URL here don't
  • 00:03:22 need a certain target object first
  • 00:03:24 we will import it or require it and we
  • 00:03:27 will also automatically create the
  • 00:03:28 database with it or the database
  • 00:03:31 connection with it so we'll create a
  • 00:03:33 variable 2 DB for database and I will
  • 00:03:35 require monk and this is actually a
  • 00:03:40 method or a function and I will call
  • 00:03:42 this function I will pass the address
  • 00:03:46 patient off my MongoDB and this is
  • 00:03:51 basically just if you remember last
  • 00:03:54 video this URL here without the MongoDB
  • 00:03:57 part at the beginning so I can't just
  • 00:04:00 copy this paste it in here that's the
  • 00:04:04 first thing the next fingers I want to
  • 00:04:06 set my default collection which you will
  • 00:04:07 use for out this file here and I
  • 00:04:10 supposed to use for data collection so
  • 00:04:11 we'll use my newly created database
  • 00:04:14 connection here and used to get mad
  • 00:04:17 which allows me to well yet a collection
  • 00:04:20 of this collection has named you for
  • 00:04:22 data that's the same name as in the last
  • 00:04:24 videos so with this we got our database
  • 00:04:28 and the connection to the database as
  • 00:04:30 well as our well collection object here
  • 00:04:33 setup so I'll start in this get method
  • 00:04:38 here in this get route in this Cathedral
  • 00:04:41 I will create a new variable data and I
  • 00:04:44 will set this equal to user data which
  • 00:04:46 is all newly created collections or say
  • 00:04:50 the representation of this collection or
  • 00:04:52 it connects you to this collection and
  • 00:04:54 here Alice has called a 5 method which
  • 00:04:57 is a method one offers us to well
  • 00:04:59 search for specific database entries
  • 00:05:02 I'll pass an empty object to tell mom
  • 00:05:06 that I want all the entries in this
  • 00:05:08 collection I could in this object well
  • 00:05:12 specify conditions which object should
  • 00:05:16 be retrieved
  • 00:05:16 like for example name column something
  • 00:05:21 if we had an object which has the same
  • 00:05:23 property and we only wanted objects with
  • 00:05:25 this name specify them but for now I
  • 00:05:28 want to retrieve all the entries all the
  • 00:05:32 documents to stay no sequel wording so
  • 00:05:38 this is the first step but this is not
  • 00:05:41 actually a list of our well database
  • 00:05:44 documents here this is indeed a promise
  • 00:05:48 and it has this on function what we can
  • 00:05:52 listen to the on success event so if we
  • 00:05:56 successfully retrieve the data
  • 00:05:58 she then fired well and our function we
  • 00:06:02 then want to execute asses of Korus
  • 00:06:04 asynchronously
  • 00:06:05 so here this function will be less
  • 00:06:08 actual documents which were retrieved
  • 00:06:10 and I can let me say that I want to
  • 00:06:13 render my in next page and I will pass
  • 00:06:17 the items just like in the last videos
  • 00:06:19 and will pass documents we retrieved
  • 00:06:23 here and I can get rid of this result
  • 00:06:25 area so if you compare this method here
  • 00:06:28 this route here
  • 00:06:30 with the same wrote in well the last
  • 00:06:33 part where we used to native mom would
  • 00:06:35 you– fredward we can see that it's got
  • 00:06:37 a lot shorter we don't have to loop
  • 00:06:39 through all the entries we don't have
  • 00:06:42 this extra area here which we use but
  • 00:06:44 instead well we simply have one call
  • 00:06:48 which kind of sets up this search query
  • 00:06:51 and then we wait for it to succeed to
  • 00:06:54 say hey what do you want to do with the
  • 00:06:56 results you get back and what we want to
  • 00:06:59 do is well we want to pass them to our
  • 00:07:03 view in this case so this was not too
  • 00:07:07 difficult I guess and inserting data
  • 00:07:09 into our database isn't much harder to
  • 00:07:12 be honest indeed it's even easier maybe
  • 00:07:16 here in the post or any insert route I
  • 00:07:21 have my item this is the same code as in
  • 00:07:25 the last videos and what I will do here
  • 00:07:27 is I will simply use my user data
  • 00:07:32 collection representation I got here
  • 00:07:34 call the insert method again a method
  • 00:07:37 provided by mom to insert data into our
  • 00:07:40 MongoDB and well I just passed the item
  • 00:07:44 I went to insert then that's it
  • 00:07:46 that really is all now I could also bind
  • 00:07:49 this statement here to a very look like
  • 00:07:52 bar insert equals and then I can listen
  • 00:07:55 to the onsuccess method if I wanted to
  • 00:07:57 do something with it but in this case
  • 00:07:58 I'm fine doing it this way in a real
  • 00:08:00 application you would of course also
  • 00:08:02 well care for error cases and handle
  • 00:08:06 them but here this is fine and the
  • 00:08:10 redirects fight you of course
  • 00:08:11 so this well just one line not too long
  • 00:08:16 next thing is updating for this we got
  • 00:08:19 two ways to do this the first way is to
  • 00:08:21 use two user data and then update the
  • 00:08:25 update method here and this method takes
  • 00:08:28 two arguments least the first argument
  • 00:08:31 will be our selector so to say which
  • 00:08:34 says which elements do you want to
  • 00:08:37 update and here I'd say I want to update
  • 00:08:40 all elements with an ID
  • 00:08:41 of and now we need this objectid thing
  • 00:08:44 again if you remember it is for in the
  • 00:08:46 last video we can do get this with a
  • 00:08:49 little helper one provides us by using
  • 00:08:51 the DB connection or monk database
  • 00:08:54 object here and called the ID method on
  • 00:08:57 it and this will allow us to transform
  • 00:09:00 any ID with this case the ID were
  • 00:09:02 retrieving from our request into an
  • 00:09:05 object ID that's the first thing this
  • 00:09:08 allows us to select data we want to
  • 00:09:12 update and the next argument is well
  • 00:09:15 what we want to put their own with what
  • 00:09:18 we want to replace this resource in this
  • 00:09:21 case our item here and the support
  • 00:09:24 should not be a comma but I : so this is
  • 00:09:27 how we can update it and we also would
  • 00:09:31 have an alternative which is easy even
  • 00:09:34 quicker got this update by ID method
  • 00:09:37 because we'll updating ready is quite a
  • 00:09:39 common case and in this case we don't
  • 00:09:41 need this DB ID thing here all we came
  • 00:09:45 here is just pass the ID nd item and
  • 00:09:47 we're done so I'm just going to comment
  • 00:09:49 out the first method even though this
  • 00:09:51 would work perfectly fine as well last
  • 00:09:54 thing deleting well not too hard here
  • 00:10:00 again we get two possibilities to do
  • 00:10:02 this like what the update we could use
  • 00:10:05 user data delete and then well manually
  • 00:10:09 do the selection with ID ID just the
  • 00:10:14 same code as with D yeah what's the
  • 00:10:17 update in here but I like to comment to
  • 00:10:20 select Picasa of course we've also got
  • 00:10:23 our user data delete by ID method here
  • 00:10:28 excuse me remove by ID and this is also
  • 00:10:34 remove not delete make sure to get this
  • 00:10:37 right so remove by DHS pass the ID and
  • 00:10:40 you are done
  • 00:10:41 of course as said before you can also
  • 00:10:44 bind all these functions all these
  • 00:10:47 database queries you two variables which
  • 00:10:50 are done promises which you can then
  • 00:10:52 handle the same
  • 00:10:53 we did here and you get data to listen
  • 00:10:54 for success or error he wins so if all
  • 00:10:58 this let me save it and I will run my
  • 00:11:01 application here and this little error
  • 00:11:04 Gator is not important at this point it
  • 00:11:06 will work nonetheless and then I will
  • 00:11:09 navigate to local over 8,000 which is
  • 00:11:13 our application here just bring them to
  • 00:11:16 some website so if we love data well we
  • 00:11:19 don't have any in the database you will
  • 00:11:24 do this I will create a new data entry
  • 00:11:28 here and now I can see we got it so
  • 00:11:31 getting an inserting that's work we can
  • 00:11:34 say this next thing is updating the data
  • 00:11:37 let me grab the side a year and I will
  • 00:11:40 name this a real title real content and
  • 00:11:45 let's capital make the first cut of max
  • 00:11:50 capital letter update load data as you
  • 00:11:54 can see updating works as well last
  • 00:11:56 thing of course is still eating paste
  • 00:11:58 the ID in here delete located it's gone
  • 00:12:01 so this are all the actions we did in
  • 00:12:04 the last to do with the native moment to
  • 00:12:05 be driver with mug and as you can see
  • 00:12:08 monk allows us to do this a little bit
  • 00:12:11 quicker and in a more convenient way
  • 00:12:13 maybe but of course it's up to you you
  • 00:12:15 may also use to normal I'm going to be
  • 00:12:19 driver which we did not last videos and
  • 00:12:21 maybe you liked us more maybe you want
  • 00:12:24 this more fine-grained control over what
  • 00:12:26 happens behind the scenes in the next
  • 00:12:30 video we will have a look at and even
  • 00:12:32 more elaborate solution to this problem
  • 00:12:35 or to the school database access thing
  • 00:12:37 which is Mongoose which will not only be
  • 00:12:40 an extra layer allowing us to do all
  • 00:12:45 these database interested quicker but it
  • 00:12:47 will be a fully fledged / M which means
  • 00:12:50 it actually Maps certain objects to
  • 00:12:55 database entries like you might know it
  • 00:12:57 from laravel Zelo went and this will
  • 00:13:00 allow us to interact with our database
  • 00:13:03 and
  • 00:13:04 some validation and defined schemas in a
  • 00:13:07 very negative way or in a very natural
  • 00:13:10 way but it will see this in the next
  • 00:13:12 video see you there but