Deleting data
By now it will probably come as no surprise that there are a few different methods for deleting data. The methods are:
remove()
: This method deletes one or more documents from the collectionfindOneAndRemove()
: This method finds a single document based on a query object and removes it, passing it to the callback function on successful completionfindByIdAndRemove()
: This method is the same as thefindOneAndRemove
method, except that it finds a document based on a provided document ID
The remove()
method can be used in two ways: as a model method, or as an instance method. When using it as a model method, you pass it a query object and it will remove the matching documents. For example:
User.remove({ name : /Simon/ } , function (err){ if (!err){ // all users with 'Simon' in their name were deleted } });
To use the remove()
method as an instance method you operate it after a find operation once you have results returned. For example:
User.findOne({ email : 'simon@theholmesoffice...