Introducing MongoDB Chapter 3
This removes the entry of a user whose name is John. The output is as follows:
As you can see in the output, the deletedCount is 1 , which means the record has been
deleted.
deleteMany()
The command for deleteMany() is the same as deleteOne(). The only difference is
that deleteOne() removes only a single entry with the matched filter whereas
deleteMany() removes all the documents which match the given criteria:
> db.users.deleteMany( { name: "Jack" } )
The output is as follows:
remove()
The remove() command works to remove a single entry, as well as multiple entries, from a
collection. If we want to remove only a single document that matched certain criteria, then
we can pass the count of entries that we wish to delete. For example, let's first create an
entry:
> db.users.insertOne({ name: 'Mike', email: '[email protected]' })
With this, now we have two entries for Mike. Now, if we want to remove just one entry
using remove(), we can do so with:
> db.users.remove({ name: 'Mike' }, 1)