Coding

Node.js + Express – Tutorial – Update and Delete Data with MongoDB

  • 00:00:00 welcome back so we're able to insert
  • 00:00:03 data to get data now let's add the
  • 00:00:06 functionality to update and delete it it
  • 00:00:09 isn't that difficult to be honest
  • 00:00:11 I'll start by copying this insert Mongo
  • 00:00:16 code here and insert it here in my
  • 00:00:20 update route and this update route of
  • 00:00:24 course also needs to have a item so I
  • 00:00:28 will add this here also copy this but we
  • 00:00:32 need one more thing to identify it we
  • 00:00:34 need an ID because we need to the ID of
  • 00:00:36 the item which should be updated so the
  • 00:00:38 item alone isn't enough because yes we
  • 00:00:40 could search for the title but we might
  • 00:00:42 have two items with the same title and
  • 00:00:44 so on so we need a unique ID that's no
  • 00:00:48 problem though it won't be submitted as
  • 00:00:50 I've set up the form accordingly and it
  • 00:00:53 will be found in the body of our request
  • 00:00:55 and will have a name of ID and does this
  • 00:00:57 because in the update form here I got
  • 00:01:01 this field here ID which has the name of
  • 00:01:03 ID so these are all day this is the data
  • 00:01:08 I need now I'm connecting to database
  • 00:01:11 but I'll replace insert one with update
  • 00:01:14 one also the parameters have to be
  • 00:01:17 changed I no longer just passed the item
  • 00:01:20 but I passed the first parameter how to
  • 00:01:24 identify deed the data to be changed and
  • 00:01:27 the second parameter what the new data
  • 00:01:30 should be like the third parameter the
  • 00:01:32 third argument is callback here will
  • 00:01:35 stay the same so the first parameter
  • 00:01:37 will be a JavaScript object where I
  • 00:01:40 describe the object I want to identify
  • 00:01:43 so in my case here I know that I want
  • 00:01:46 the ID underscore at least default ID
  • 00:01:48 MongoDB inserts to be equal to the ID I
  • 00:01:54 extract from the request but this would
  • 00:01:57 not work because IDs in MongoDB in the
  • 00:02:02 database are actually of type object ID
  • 00:02:06 they are not strings and MongoDB is a
  • 00:02:10 strongly typed database so types are
  • 00:02:13 important
  • 00:02:14 therefore I need to make this an object
  • 00:02:17 ID to to do this I will scroll to the
  • 00:02:21 top duplicate this line where kram one
  • 00:02:25 would be and by the way this is an error
  • 00:02:27 here this should be Mongo client as I
  • 00:02:30 saw it did work nonetheless but it
  • 00:02:33 should be Mongo client and here will
  • 00:02:36 access object ID and this will be called
  • 00:02:41 var object ID and now what I can do is I
  • 00:02:48 can transform this ID to an object ID by
  • 00:02:51 just using this variable I just created
  • 00:02:53 where I access this object ID in our
  • 00:02:56 MongoDB package and well use it as a
  • 00:03:01 function where I pass this ID and this
  • 00:03:03 will transform this ID into an object ID
  • 00:03:06 which has done a correct type for
  • 00:03:07 MongoDB to use it the next thing is the
  • 00:03:10 data I want to change I'll have a child
  • 00:03:13 object here too and I will use dollar as
  • 00:03:17 set does the property name MongoDB needs
  • 00:03:20 to know okay now after the colon follows
  • 00:03:23 what the new item should be or what the
  • 00:03:26 new item is in this case item of course
  • 00:03:29 so after dollar set here I have two new
  • 00:03:32 data which will replace the old data
  • 00:03:35 identified with the identifier specified
  • 00:03:38 here as a first argument that's very
  • 00:03:40 important to get right first argument
  • 00:03:42 identifies data second argument after
  • 00:03:46 the set property specified specifies
  • 00:03:50 what a new data should be so this will
  • 00:03:53 replace any item or there will be only
  • 00:03:57 one item with this ID with this item
  • 00:04:00 last fingers I will update this text
  • 00:04:03 here safe does restart my server and
  • 00:04:07 then let's have oke I'll just copy this
  • 00:04:11 ID here paste it here and give this some
  • 00:04:15 title here as well as some content and
  • 00:04:18 some author update this and load data as
  • 00:04:23 you can see this was updated so this
  • 00:04:25 worked great
  • 00:04:27 next thing is last thing I want to be
  • 00:04:29 able to delete data for this I will copy
  • 00:04:33 this part from the update route because
  • 00:04:37 I will of course also have an ID which
  • 00:04:39 identifies the data what to delete but
  • 00:04:42 here I will replace update 1 with delete
  • 00:04:45 1 and I will get rid of the second
  • 00:04:49 argument because I don't need any new
  • 00:04:51 data there won't be new data we'll just
  • 00:04:53 delete it
  • 00:04:53 but I'll keep the first parameter which
  • 00:04:56 identifies the object to be deleted and
  • 00:04:59 that's really all now I just replace
  • 00:05:02 this console.log here
  • 00:05:04 reload let's copy this ID insert here
  • 00:05:07 click delete and load data as you can
  • 00:05:10 see it's gone and this is how you update
  • 00:05:13 and delete no as you probably saw when I
  • 00:05:16 started typing and you saw my auto
  • 00:05:18 completion there are a lot more
  • 00:05:21 operations you can use with MongoDB and
  • 00:05:24 I recommend you just check the official
  • 00:05:26 documentation for this and of course you
  • 00:05:28 will see more in future videos but to be
  • 00:05:33 honest as you probably saw this is kind
  • 00:05:36 of annoying to do this all by hand
  • 00:05:38 every time and yes we could refactor
  • 00:05:41 this a bit but there are great
  • 00:05:42 extensions our packages which build up
  • 00:05:45 upon the default MongoDB driver and will
  • 00:05:49 give us a more convenient way of
  • 00:05:51 interacting with or among would it be
  • 00:05:52 database and we will see this in the
  • 00:05:55 future videos bye