It's time to move on to that final crud operation which is D for delete now like with create read and
update we'll be able to delete individual documents or we can batch delete multiple at the same time
the two methods we're gonna use to get this done are under D for delete in the API docs we have delete
many for deleting multiple and delete one for just removing an individual one.
Now in the past the method you used to remove data was actually called remove which is down below.
That's been deprecated and delete many and delete one are now the future of deleting data with Mongo
D.B..
So we're gonna go ahead and kick things off with delete one.
Now the API for both is very similar.
We provide the filter to target the document or documents.
We want to remove.
Then we have the options which we're not going to provide as they're not needed.
And we have the callback which we're also not going to provide since we'll use a promise instead.
So for delete 1 and delete many all we really have to do is provide these search criteria similar to
what we did with find and find one.
Let's go ahead and move into Mongo D.B..
J asked to mess around with this.
I'm going to remove our calls to update many and Update 1 and we will start by using delete many to
delete multiple documents from that user's collection.
What I'm going to do is remove all of the users who have the age of twenty seven.
So right here if I refresh things there are two.
I have Andrew and I have Gunther both with twenty seven as the age.
My goal is to delete all of those with one single operation.
So for Mongo D.B. J S will start off like we've done plenty of times before.
D.B. dot collection we're looking for the user's collection and we will be using delete many to delete
multiple documents and once again the only argument we're going to provide is the first one where we
provide our search criteria.
In this case we are looking to delete documents where the age is twenty seven.
Now if I wanted to target two things I could add multiple properties onto here like for example deleting
people whose name is Andrew and age is twenty seven.
In this case we just want to delete based off of this one field.
Now from here we can go ahead and set up our call to then.
So right here we'll get access to the result.
If we were able to perform the operation and we can also set up our call to catch and here we'll get
access to the error.
If we were not able to perform the operation and once again we'll just log both to the screen console
dialogue printing result and then down below console dialog printing the error.
Now with this in place we're actually all done.
There's not a whole lot to deleting data and we can go ahead and run the script.
What I'm gonna do is shut it down and using control C and then I'll start it up once again using up
and enter.
That's going to rerun things and we can see that on the result.
We have a whole bunch of different pieces of information but I also have deleted account which is showing
too.
So this is the main result we get back allowing us to confirm the number of documents that were removed.
Right here I can go ahead and refresh the user's collection and I can see that both of those are now
gone.
So there's no longer anyone with the age of 27 in the database.
They've all been deleted.
So with this in place we know how to use the delete many function to delete multiple documents.
Now we're going to move on to the challenge for the video where it's up to you to use delete one to
delete an individual document.
Right here I have the challenge comments for this one.
Your goal is to use delete one to remove a single task.
First up you're gonna ground the description for the task you want to remove.
So over inside of robo 3 T you could remove any of your tasks by their description.
So I have these three right here as options for me.
Now once you've picked the description go ahead and copy that to the clipboard and set up your call
down below so you can refer to the documentation for delete one to figure out how it's supposed to be
used.
Hint It's basically the same as delete many.
Now once you found that Colin place you are going to not provide the callback and instead opt for the
promise.
So toss on the then and catch method calls to set up your handlers and finally test your work rerun
the script and make sure the one task you targeted is gone and that the others are still in place before
I set you free.
I will comment out our other call up above and down below.
Right here you can add the challenge code.
All right.
Pause the video.
Knock that out when you're done come back and click play
out that go.
Let's go ahead and kick things off by grabbing the description for the document.
I'm gonna go ahead and remove clean the house.
Now it's already done.
So I don't need to keep it around right here.
I'll grab its description.
Next up I'm going to bring that over to my code and I'm going to call the correct method.
That's going to be D.B. dot collection.
I'm targeting that tasks collection and I will use delete one to delete just an individual item right
here.
I provide my query I am looking for the task where description equals a specific value.
The string I just copied it to the clipboard.
Perfect.
Now from here we can go ahead and move on to the next step which is to add our promise method calls.
Now there are so many ways we can structure code when we have a lot of chaining going on like this.
I could take delete one and move that down to the next line.
That would be a valid way to get the job done.
Or I could go ahead and do something similar to what we've been doing before and simply break up the
argument like this.
And then toss on then setting up my callback right here.
I'll get the result and I'll go ahead and dump that to the console and then down below that we'll make
sure we use catch to handle those potential errors right here.
I can call that E for short.
Or we could just listed out in its entirety error.
And once again I'll just log that.
All right.
All done.
Let's go ahead and test things out.
I'm gonna remove the challenge comments.
I'm going to save the file and before I rerun it I'll just refresh the data to make sure clean the house
is still showing up and it is from the terminal.
I'll shut down the previous script.
Start it up one more time.
Right here I can see I have a delete count of one and if I refresh the tasks collection I can see that
first document is gone.
So there we have it.
We now know how to create data read data update data and delete data.
Now the goal of this section wasn't to start building out our next app which is that task manager REST
API.
The goal here was to just get comfortable working with Mongo D.B. and promises so we can use these together
with Express to build out a more complete and real world application trying to learn about Mongo D.B.
while also integrating it into an application would would've made it for quite a difficult journey.
That's it for this video and that's it for this section.
I'll see you in the section introduction for the next one which is coming up now.