Chapter 8. Interacting with Data – Deleting

We have learned about Creating, Reading, and Updating data using Mongoose. Now it is time to look at the final CRUD operation: deleting.

In this chapter we will:

  • Learn how to delete multiple documents in one operation
  • Learn how to delete a single document
  • Put this knowledge into action by adding the functionality to delete a user account

By the end of this chapter, you will be familiar with the different approaches you can take to delete data, and when it is best to use each one. You will also have added the ability to delete users and projects from your MongoosePM application.

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 collection
  • findOneAndRemove(): This method finds a single document based on a query object and removes it, passing it to the callback function on successful completion
  • findByIdAndRemove(): This method is the same as the findOneAndRemove 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 : '[email protected]'} , 
  function (err,user){
    if (!err){
      user.remove( function(err){
        // First user with matching email address was removed
      });
    }
});

Note

Be careful when using <Model>.remove() (where <model> is the model name). If you specify an empty query or just a callback—like you would when using the instance method—then all the documents in the collection will be removed.

Both the findByIdAndRemove() and findOneAndRemove() methods accept options and can return the deleted document to the callback. We're going to use the findByIdAndRemove method shortly. So let's have a look at findOneAndRemove instead. The accepted parameters are as follows:

  • Query object: This is for a document to find and remove.
  • Options: This optional parameter is an object with two possibilities:
    • sort: set the sort order in case multiple docs are found
    • select: set the document fields to return in the object to the callback
  • Callback: This optional parameter This returns either an error or the deleted document.

Here's a quick example:

User.findOneAndRemove(
  {name : /Simon/},
  {sort : 'lastLogin', select : 'name email'},
  function (err, user){
    if (!err) {
      console.log(user.name + " removed");
      // Simon Holmes removed
    };
  }
);

Here we search for users whose name contain Simon, and order them by the date they last logged in, before removing the user who hasn't logged in for the longest time. Once removed, we send their name and e-mail address to the callback to output confirmation of the deletion.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.109.151