CRUD Operations: Set
🚀 Goal: Update Any Field with $set—Like a Pro
Your platform is thriving, and your users expect flexibility—whether it’s updating a property’s title, adding a new amenity, or correcting a typo. As the backend engineer, you’re the one who makes these real-time updates possible, keeping your data fresh and your users happy.
In this exercise, you’ll harness the power of MongoDB’s $set
operator to update any field in your documents. This is how you give your platform the agility to grow and adapt—one update at a time.
đź§© Exercise: Update a Document
-
Open the File
Navigate toserver/src/lab/
and opencrud-6.lab.js
. -
Locate the Function
Find thecrudUpdateElement
function in the file. -
Shape the Update
- The function receives three parameters:
id
: The document’s_id
key
: The field name to updatevalue
: The new value to set
- Use
$set
to update the specified field with the new value—dynamic, flexible, and powerful!
- The function receives three parameters:
🚦 Test Your API
- Go to
server/src/lab/rest-lab
directory. - Open
crud-6-lab.http
. - Click Send Request to execute the API call.
- Verify the response shows the document with the updated field.
🖥️ Frontend Validation
Edit a field (like the Title) of a listing in the app and watch your changes stick—even after a refresh.
Slick, instant, and oh-so-satisfying!
With this step, you’re not just updating data—you’re keeping your platform fresh, relevant, and responsive to every user’s needs.
Ready to give your data a makeover? Let’s get started!