Coding

Node.js + Express – Tutorial – Mongoose

  • 00:00:00 hi everyone welcome back in the last
  • 00:00:03 video we had a look at monk which allows
  • 00:00:05 us to interact or a MongoDB a little bit
  • 00:00:08 easier or by writing less code by giving
  • 00:00:12 us this extra layer so to say which we
  • 00:00:15 can access which takes a lot of work off
  • 00:00:19 of us and dust is not background in this
  • 00:00:21 video we'll have a look at Mongoose and
  • 00:00:24 Mongoose is not directly comparable to
  • 00:00:28 monk it is a lot more than monk monk is
  • 00:00:32 just you might say an extension to the
  • 00:00:33 MongoDB driver mangu is it has a
  • 00:00:36 different concept it is a fully pledge
  • 00:00:39 term Orion which means that to put it
  • 00:00:44 easy it will represent our data our
  • 00:00:49 documents in our no sequel database as
  • 00:00:52 JavaScript objects when you find a
  • 00:00:55 schema for a certain will or check or
  • 00:00:58 how our entries in the database should
  • 00:01:00 look like and then we create models of
  • 00:01:03 this schema and then we use these models
  • 00:01:07 to uh create childhood objects and to
  • 00:01:10 work with these objects in a very
  • 00:01:12 natural way and all our changes we do
  • 00:01:16 there are then upon certain commands
  • 00:01:19 written to the database and reflect
  • 00:01:21 there and we can work with it on a very
  • 00:01:26 and yet your natural or high level way
  • 00:01:30 without having to doing all the
  • 00:01:33 nitty-gritty things ourselves the best
  • 00:01:35 thing to learn it is to actually see it
  • 00:01:37 so let's dive into it what I will do is
  • 00:01:40 I will take my model to be off draw
  • 00:01:42 project here duplicate it and just
  • 00:01:46 rename it and once this is done come on
  • 00:01:52 um I will go to my package to chasten
  • 00:01:55 file I'll remove MongoDB we won't me
  • 00:01:58 that human who's has all this built in
  • 00:02:00 and then I'll stop my server here I will
  • 00:02:06 well navigate into this newly created
  • 00:02:09 folder
  • 00:02:10 and here I will install with the save
  • 00:02:16 flag would say flag Mongoose so with
  • 00:02:23 this I got Mongoose and stalled as you
  • 00:02:25 can see now in the package to chase and
  • 00:02:27 fall and to use it I'll go in my routes
  • 00:02:30 in the excess fat here again I will get
  • 00:02:33 rid of the MongoDB related things here
  • 00:02:37 and of course I will also get rid of all
  • 00:02:41 the code here all the well basically
  • 00:02:45 everything related to MongoDB to be in
  • 00:02:48 our Ross okay so now I get this empty
  • 00:02:51 app again and now let's use Mongoose the
  • 00:02:55 first thing I will do is at the top of
  • 00:02:57 course I will require it so that we can
  • 00:03:00 use it require and then Mongoose and
  • 00:03:05 then I want to establish a connection
  • 00:03:07 and keep this connection open throughout
  • 00:03:10 this file here so I will do it here at
  • 00:03:12 the top by using this Mongoose object
  • 00:03:16 here this variable and then calling the
  • 00:03:18 connect method on it and then as an
  • 00:03:22 argument I have to provide the power to
  • 00:03:25 the database and this path we can use it
  • 00:03:28 here from the monke project has the same
  • 00:03:31 well form as it does here so no mom
  • 00:03:36 would be colon slash slash slash slash
  • 00:03:38 the beginning just the direct address of
  • 00:03:41 our database so now this is the
  • 00:03:46 connection to our MongoDB and now it is
  • 00:03:49 open and we can work with it the next
  • 00:03:53 thing I'll do is as I already said it's
  • 00:03:57 all about creating this schemas which
  • 00:03:59 kind of tell mom who is how the data in
  • 00:04:03 the database should look like how it is
  • 00:04:05 how is it is structured therefore I will
  • 00:04:08 create a new variable here called schema
  • 00:04:10 and it does we'll be learning what that
  • 00:04:14 will be related refer to our mongoose
  • 00:04:19 schema object here and whatever
  • 00:04:23 next is I will define the schema or a
  • 00:04:26 user data you should have so I will call
  • 00:04:29 this user data scheme and with this
  • 00:04:32 create a new schema and here I just
  • 00:04:38 passed as an argument to the constructor
  • 00:04:40 well a child script object which defines
  • 00:04:43 this schema and in this case we got a
  • 00:04:47 title pick content and offer all free
  • 00:04:49 will be strings I will set title which
  • 00:04:52 should be a string I will set content
  • 00:04:55 which should be a string and author
  • 00:04:57 now the cool thing is this schema will
  • 00:05:01 define how Mongoose will writes the data
  • 00:05:04 to the database of course but it will
  • 00:05:07 also do some validation now not
  • 00:05:10 regarding these types here but for
  • 00:05:13 example let's say we want this title to
  • 00:05:16 be required I would replace it with a
  • 00:05:20 separate JavaScript object we would have
  • 00:05:22 first we'll type is string and then I
  • 00:05:24 would set required true now of course
  • 00:05:28 you know whatever which options you have
  • 00:05:30 of specifying here should have a look at
  • 00:05:33 the Mongoose documentation because
  • 00:05:35 Mongoose is a bit too complex to cover
  • 00:05:37 is completely in one video but this is
  • 00:05:40 how I generally work with it and how you
  • 00:05:42 define these schemas and why they are so
  • 00:05:45 important because now we won't be able
  • 00:05:47 to save something to the database which
  • 00:05:50 doesn't specify a title because we're
  • 00:05:53 saying the title is required
  • 00:05:55 it may not be empty or not okay so this
  • 00:05:59 is this schema I define now this would
  • 00:06:03 work like this but I will come back to
  • 00:06:06 it in a minute but first let me continue
  • 00:06:09 after creating the schema I will now
  • 00:06:12 create a model of that schema so this
  • 00:06:15 schema is just like the blueprint and
  • 00:06:18 now I will create a new model which I
  • 00:06:21 will hold
  • 00:06:22 use your data and this will be pipe
  • 00:06:26 Mongoose model here we call this method
  • 00:06:31 and here I first pass the name of this
  • 00:06:34 model and then
  • 00:06:37 the schema which should be used as a
  • 00:06:39 blueprint for this model so just to get
  • 00:06:43 this differentiation clear here which
  • 00:06:46 just you find the layout whereas here
  • 00:06:49 we're then creating an actual model of
  • 00:06:51 debt which you can later use to
  • 00:06:53 instantiate it and to actually write
  • 00:06:56 data to the database and this is if you
  • 00:06:59 watch my letter will produce highly
  • 00:07:01 comparable to levels l went to school
  • 00:07:04 concept now with dad what mongers would
  • 00:07:08 do it would create and store this data
  • 00:07:12 at a collection called user data with an
  • 00:07:15 S at the end to the plural form of this
  • 00:07:18 model name now we already have our
  • 00:07:21 collection called user – data course we
  • 00:07:25 could create a new one here but if you
  • 00:07:27 want to force a certain collection name
  • 00:07:30 you may pass an additional JavaScript
  • 00:07:32 object to this schema constructor here
  • 00:07:36 which will contain some options in this
  • 00:07:40 case I'd only specify one option which
  • 00:07:42 is collection the collection key and I
  • 00:07:46 defined that the collection name will be
  • 00:07:48 user data now this will override the
  • 00:07:50 default of taking this in the plural
  • 00:07:53 form and our collection will be user
  • 00:07:56 attached data which was before in the
  • 00:07:59 past videos so this is how we set up the
  • 00:08:02 schema and how we use it in our model
  • 00:08:04 and now let's do the actual database
  • 00:08:06 operations and how this works first
  • 00:08:10 let's have a look at getting some data
  • 00:08:12 here what I want to do is I will call
  • 00:08:16 user data our model here and through
  • 00:08:20 this more like and directly code find
  • 00:08:22 with no arguments to find all entries
  • 00:08:25 and then Mongoose provides us the Len
  • 00:08:30 function to basically handle the results
  • 00:08:34 when it's done when when say it is done
  • 00:08:36 with the database operation and in there
  • 00:08:40 a loop as function which has the
  • 00:08:42 retrieved documents and in this function
  • 00:08:47 I can then call my well let's render
  • 00:08:50 that you returned the response and
  • 00:08:53 Renard is index file here and set the
  • 00:08:56 items equal to the documents we
  • 00:08:58 retrieved here this is all this is how
  • 00:09:01 it works and that's why I meant with
  • 00:09:04 abstracting this away yes in a way of
  • 00:09:07 course looks like a database access here
  • 00:09:09 with five method but we are using our
  • 00:09:11 setup model here and you would set up
  • 00:09:14 such a model for each the collection
  • 00:09:19 type where each yet each collection we
  • 00:09:21 have in our database like again if
  • 00:09:24 you're familiar with this with a low
  • 00:09:27 Quint and narrow so that's how we find
  • 00:09:30 or get our data next let's have a look
  • 00:09:34 at storing it well for this I will trade
  • 00:09:38 a new variable called data which is a
  • 00:09:41 new user data now we're using our model
  • 00:09:45 to create an instance of it I can then
  • 00:09:49 pass my item which I define up here into
  • 00:09:53 the constructor of this model to
  • 00:09:56 automatically assign all these fields
  • 00:09:58 because of course enters this import
  • 00:10:00 this has the same structure as our
  • 00:10:01 schema that's why would you find the
  • 00:10:03 schema so now I have two data here and
  • 00:10:07 then I just called data save and this
  • 00:10:09 will also store it to the database know
  • 00:10:12 insert or anything just pay the save
  • 00:10:14 this will do that if you have a look at
  • 00:10:20 the update method here here I would
  • 00:10:23 again use my model that I would call
  • 00:10:26 find to find the edge we want to up it
  • 00:10:29 and I can use defined by ID method here
  • 00:10:32 to make it easy and just pass in the ID
  • 00:10:35 and the psyche may be a number or a
  • 00:10:38 string or an object ID and also
  • 00:10:40 automatically figure out how it has to
  • 00:10:43 transform it to actually get our
  • 00:10:46 database entry here so I also provide a
  • 00:10:51 callback here and this callback will
  • 00:10:55 have given an error or the documents it
  • 00:10:58 found and now we could just show an
  • 00:11:00 example and
  • 00:11:01 sure the real app handle this error by
  • 00:11:04 while outputting error no entry found
  • 00:11:09 and then of course we should also render
  • 00:11:12 back some error response in this case
  • 00:11:13 but here I will just continue and what
  • 00:11:16 it will do is never will update this
  • 00:11:18 document so we'll set doc title and I'm
  • 00:11:23 getting this auto-completion up because
  • 00:11:26 this is the schema we defined and I will
  • 00:11:29 set it to well basically whatever we
  • 00:11:34 have stored in our request here and
  • 00:11:36 therefore I can get rid of this item
  • 00:11:37 here also update the content and finally
  • 00:11:46 I will set dot offer equals request body
  • 00:11:50 offered and now I call dog save to save
  • 00:11:55 this but not as a new entry but just to
  • 00:11:58 update this entry with new information
  • 00:12:02 finally let's go to removing and
  • 00:12:06 elements in our database
  • 00:12:08 this is very easy can use again or
  • 00:12:11 morally user data and then the find my
  • 00:12:14 ID and remove method and while this does
  • 00:12:17 what the name implies I pass an ID but
  • 00:12:20 now to execute this I will call the exit
  • 00:12:24 function at the end because I'm not
  • 00:12:26 fetching any call back here I'll just
  • 00:12:30 leave it like this of course I also
  • 00:12:36 deleted this redirect I just saw so I
  • 00:12:40 definitely want to add this here and
  • 00:12:43 here and now let me restart this server
  • 00:12:48 for this new folder here and I already
  • 00:12:52 had one entry here because I test this
  • 00:12:55 but let me create in you and you entry
  • 00:12:59 with new content bunny injured and get
  • 00:13:04 data and I can see it here and now let's
  • 00:13:06 update this first entry look let's say
  • 00:13:12 alson you you you
  • 00:13:14 Update load as you can see this works
  • 00:13:18 and finally to get rid of it
  • 00:13:20 let's try this out voice so that's how
  • 00:13:23 we use Mongoose and now I'll be honest
  • 00:13:25 Mongoose has a lot more going on that
  • 00:13:28 one than what I showed you here you can
  • 00:13:30 also use relations or as it's called in
  • 00:13:34 the new snow sequel world populations to
  • 00:13:36 relate certain collections you can do a
  • 00:13:40 lot more with these models they really
  • 00:13:44 have a great validation hope in and so
  • 00:13:46 on and this might be something for
  • 00:13:49 future videos but this here should get
  • 00:13:51 you going and help you get started
  • 00:13:54 Mongoose in the description you will
  • 00:13:56 find a link for more information if you
  • 00:13:59 want to dive in a little deeper right
  • 00:14:01 now see you in the next videos bye