So back in our mongo shell with the data I also worked on in the last modules.
Now for what I'm showing you, you can use with any data you have,
you don't need specific data.
So here I happen to still have my users collection I worked with in the update module
but again it's fine if you have different data or the same collection but the data looks different, that's
all fine.
Let's say we want to delete Chris, now
to delete
Chris, what you can do is you can use your collection and then use delete one if you know it'll be only
one document you want to delete.
Now you need to specify a query selector, a filter here and the filter you specify here is the same as
for updating and finding documents,
everything you learned in the read module will work here, whatever you need to identify a document, with
how many conditions you might need is fine.
If you check the age and the name and let's say the hobbies all in one query selection, that is fine,
you can do that,
you simply need to narrow down the document or the documents you want to delete.
So a simple check is of course to look for
name equal to Chris and that is it,
you can specify a second argument where you can set a write concern if you want to but this is not required
if you're fine with the default write
concern as I am for the rest of this course. So I can just hit enter
and you see, one document was deleted
and unsurprisingly if we now look into all users, we'll see Maria, Anna, Manuel and Max but Chris is
gone.
Now with Chris being gone and delete one being used,
let me use delete many to delete all persons who are sporty and have the age greater than 30.
So for this, we can again use our collection and then delete many
because here I know I want to delete multiple records,
this can still delete only one if only one record should match your condition
but unlike delete one which will then only just delete the first record that matches, even if you had
multiple documents that would match,
this delete
many will always delete all matching documents.
So here I can say age should be greater than 30 and isSporty
should be true,
this is now my condition.
If I hit enter,
nothing was deleted
so let's quickly check,
well it's total age of course, not age,
my bad,
so let's quickly change this to total age and still, nothing was deleted.
Well the reason for this is simple,
we had no matching document.
Maria is simply younger, Anna doesn't have an age, Manuel would have an age but is not sporty and Max is
sporty but doesn't have an age.
So what we can do is we can of course delete all persons who are sporty and where total
age does not exist,
so where exists is false,
you learned about exists in the read section too.
Now if I hit enter, two documents were deleted,
so let's quickly have a look at our collection and
we see Anna is still there because she doesn't have an age, she's sporty but she doesn't have an age
and total age
null as you learn then the read section does not suffice for exists
false because the field does exist,
it's null but it does exist.
Manuel is also still there
but Max and Maria were deleted because they were sporty and they had no age field
by the looks of it.
So this is delete many in action
and as you see, here delete many taking into account two different query selectors,
of course you can add as many as you need
as I mentioned.