Delete Many Documents with model.remove()
Relevant Links
Hints
Hint 1
You just need a condition, and a callback with error and response .
Solutions
Solution 1 (Click to Show/Hide)
const removeManyPeople = (done) => {
const nameToRemove = "Mary";
Person.remove({name: nameToRemove}, (err, response) => {
if(err) return console.log(err);
done(null, response);
})
};
Code Explanation
-
remove()sends a remove command to MongoDB to remove all matched documents. In this case, all documents with anameofMaryare removed. - The response is a JSON object containing properties such as
deletedCount- containing the number of documents removed from the collection.