Skip to content

Mutation Patterns Summary

In this chapter, we stepped beyond reading data and took control of our database. We established an Admin Area to organize our mutating endpoints, and we tackled the two most common ways web applications change data.

Deletions are blunt operations, best handled quickly and efficiently.

  • The Trigger: Client-side JavaScript (fetch()) sends an HTTP DELETE request.
  • The Router: Pulls the :id from the URL parameters (req.params.id).
  • The Ops Layer: Executes Mongoose Model.findByIdAndDelete(id).
  • The Feedback: The router returns JSON (res.json()); the client removes the HTML element from the screen without refreshing the page.

Updates require precision, validation, and a focus on UX.

  • The Trigger: A standard HTML <form> submits an HTTP POST request (the browser doesn’t easily natively support HTTP PUT or PATCH from a standard HTML form element without JS).
  • The Router: Pulls both the :id from the URL and the new data from the form (req.body).
  • The Ops Layer: Executes the Load → Modify → Save cycle (findById() → reassign variables → save()).
  • The Feedback: If successful, the router redirects the user back to a view where they can see the change (res.redirect()). If it fails, the router re-renders the form view, pre-populating it with the user’s failed data so they can try again.
Professor Solo

Understanding when to use client-side fetch (for invisible, layout-preserving actions like delete) versus when to use standard form submissions (for heavy data entry that might need server-side error re-rendering) is the mark of a senior engineer.

Completed Update and Delete Demo Repository


We now have isolated Contacts and isolated Projects. But in real applications, data is intertwined. Let’s learn how to link documents together.