CRUD Operations: Push
🚀 Goal: Effortlessly Add to Arrays with $push
Your platform is thriving, and guests are eager to share their experiences. Imagine a traveler leaving a glowing review after a perfect stay, or a host receiving valuable feedback. As the backend engineer, you make these moments possible—instantly updating listings with new reviews and keeping your data dynamic.
In this exercise, you’ll use MongoDB’s $push
operator to add reviews (or any array item) to your documents. With every new review, your platform becomes richer and more engaging for everyone.
đź§© Exercise: Add a Review to an Array
-
Open the File
Navigate toserver/src/lab/
and opencrud-7.lab.js
. -
Locate the Function
Find thecrudAddToArray
function in the file. -
Update the Code
- Use
$push
to add the new review to thereviews
array. - The function receives two parameters:
id
: The document’s_id
review
: The review object to add
- (Bonus) Use
$inc
to increment thenumber_of_reviews
field by 1.
- Use
🚦 Test Your API
- Go to
server/src/lab/rest-lab
directory. - Open
crud-7-lab.http
. - Click Send Request to execute the API call.
- Verify the response shows the updated document with the new review.
🖥️ Frontend Validation
Add a new review in the app and watch it appear instantly for the selected listing—smooth, dynamic, and oh-so-satisfying! This is how you keep your platform lively and your users engaged.
With this step, you’re not just updating arrays—you’re capturing the stories and feedback that make your platform come alive.
Ready to let your users’ voices be heard? Let’s get started!