Instructor: Finally, let's learn
how to delete documents in MongoDB.
So just like before,
we have delete one to delete one single document,
and we have delete many, well,
to delete multiple documents at the same time.
Alright, and just like before,
delete one will only work
for the first document matching your query.
And delete many will of course
work for all the documents matching your query.
Okay.
Just to get an overview of what we have here
at this moment in time,
let's do a find of all the tours.
And what I'm gonna do now
is to delete all the tours
which have a rating less than 4.8.
So that's gonna be just the first one
but if we had multiple tours with that condition,
then all of them would be deleted.
So db.tours.deleteMany.
So the condition is rating...
Open a new object
so that we can use the less than operator and 4.8.
Close it and then close this one.
And run this and here we see that it's deleted one document.
Right away it's telling us here
how many documents were affected by this operation.
So if we do this find now,
well then the first one that we had is gone.
Alright, so that's no problem.
We are only really using this here to play around
and so we can delete as much as we want here.
No problem.
Now if you wanna delete
all of the documents in the collection,
you would do it like this.
So deleteMany.
And I'm not gonna execute this.
But you would do it like this.
So all you have to do is pass in the empty object
because the empty object is basically a condition
that all of the documents always match.
It would be like simply using an empty object here in find.
I'm not gonna do that
and you should be careful with it,
especially when doing it in a real app
because you cannot come back from this operation.
So unless you have some backup in place,
this will effectively delete all the documents
in your collection,
in this case the tours collection.
So I'm not performing that.
I just wanted to show you
that this is how we delete everything.
So this is the simplest operation
and we don't use it all that often.
But I just wanted to have a short video
just to demonstrate that it also exists.