Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mongoose for Application Development

You're reading from  Mongoose for Application Development

Product type Book
Published in Aug 2013
Publisher Packt
ISBN-13 9781782168195
Pages 142 pages
Edition 1st Edition
Languages
Author (1):
Simon Holmes Simon Holmes
Profile icon Simon Holmes

Table of Contents (18) Chapters

Mongoose for Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Introducing Mongoose to the Technology Stack Establishing a Database Connection Schemas and Models Interacting with Data – an Introduction Interacting with Data – Creation Interacting with Data – Reading, Querying, and Finding Interacting with Data – Updating Interacting with Data – Deleting Validating Data Complex Schemas Plugins – Re-using Code Index

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 : 'simon@theholmesoffice...

CRUD – deleting user and projects


That's the theory, so let's see it in action and let a user delete their account. To follow the approach we've been taking through this application, we'll have a page displaying a form and a form handler.

The routes in app.js that we'll use for this are:

app.get('/user/delete', user.confirmDelete); // delete current user form
app.post('/user/delete', user.doDelete);     // Delete current user action

The "Are you sure" page

Before a user deletes the account we want to ask them to confirm this, in case they clicked on the link by accident. So we create a new view as views/user-delete-form.jade:

h1= title
p #{name}, are you sure you want to permanently delete your account? The email address on this account is #{email}
form(id="formUserDelete", method="post", action="")
  input(type="hidden", name="_id", id="_id", value="#{_id}")
  input(type="submit", value="Yes, please delete it!")

And create a controller to map it to the route in routes/user.js:

// GET user delete...

Summary


In this chapter, we have learned how to delete database records using Mongoose methods. We then applied this knowledge to our project and added the ability for a user to delete their profile.

This brings us to the end of the data interaction CRUD section, so you should now have a good idea of how to do most things you want to with data in your applications.

Coming up in the next chapter, we're going to look at validation and see how Mongoose can help us maintain data integrity.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mongoose for Application Development
Published in: Aug 2013 Publisher: Packt ISBN-13: 9781782168195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}