Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Updating Documents with Express
📖 Scenario: You are building a simple web server that manages a list of books. Each book has a title and an author. You want to update the author of a specific book using Express.
🎯 Goal: Create an Express server with a route to update the author of a book by its title.
📋 What You'll Learn
Create an array called books with three book objects, each having title and author properties.
Create a variable called targetTitle with the value of the book title to update.
Use Array.prototype.find() to locate the book object with the title matching targetTitle and update its author property.
Add an Express PUT route at /books/:title that updates the author of the book with the given title.
💡 Why This Matters
🌍 Real World
Updating documents or records is a common task in web servers managing data like books, users, or products.
💼 Career
Understanding how to update data in Express routes is essential for backend development roles.
Progress0 / 4 steps
1
Create the initial books array
Create an array called books with these exact objects: { title: '1984', author: 'George Orwell' }, { title: 'Brave New World', author: 'Aldous Huxley' }, and { title: 'Fahrenheit 451', author: 'Ray Bradbury' }.
Express
Hint
Remember to create an array named books with three objects inside.
2
Set the target book title to update
Create a variable called targetTitle and set it to the string '1984'.
Express
Hint
Use const to create targetTitle and assign '1984'.
3
Find and update the book author
Use const bookToUpdate = books.find(book => book.title === targetTitle) to find the book. Then set bookToUpdate.author = 'Eric Arthur Blair' to update the author.
Express
Hint
Use find() to get the book, then assign the new author.
4
Add Express PUT route to update author
Add an Express PUT route at /books/:title. Inside the route, find the book by req.params.title and update its author with req.body.author. Send back the updated book as JSON.
Express
Hint
Use app.put with req.params.title and req.body.author to update and respond.
Practice
(1/5)
1. What does the findByIdAndUpdate method do in Express with Mongoose?
easy
A. It updates a document by its ID and returns the updated document.
B. It deletes a document by its ID.
C. It creates a new document with a given ID.
D. It finds a document but does not update it.
Solution
Step 1: Understand the method purpose
findByIdAndUpdate is used to find a document by its ID and update it with new data.
Step 2: Check the return value
When used with { new: true }, it returns the updated document, not the old one.
Final Answer:
It updates a document by its ID and returns the updated document. -> Option A
Quick Check:
Update document by ID = A [OK]
Hint: Remember: findByIdAndUpdate updates and returns new data [OK]
Common Mistakes:
Confusing update with delete operation
Expecting it to create a new document
Thinking it only finds without updating
2. Which of the following is the correct syntax to update a document by ID and return the updated document in Express with Mongoose?
easy
A. Model.findOneAndUpdate(id, update);
B. Model.updateById(id, update, callback);
C. Model.update(id, update, { returnNew: true });
D. Model.findByIdAndUpdate(id, update, { new: true }, callback);
Solution
Step 1: Identify correct method and parameters
The correct method is findByIdAndUpdate with parameters: id, update object, options, and callback.
Step 2: Confirm option for returning updated document
The option { new: true } ensures the updated document is returned.
Final Answer:
Model.findByIdAndUpdate(id, update, { new: true }, callback); -> Option D
Quick Check:
Correct syntax includes { new: true } = C [OK]
Hint: Use { new: true } option to get updated document [OK]
Hint: Add { new: true } to get updated document in callback [OK]
Common Mistakes:
Assuming updateById is a valid method
Expecting updated doc without { new: true }
Thinking update object must be string
5. You want to update a user's email only if the new email is not empty and valid. Which approach correctly updates the document safely in Express with Mongoose?
hard
A. Check email validity before calling findByIdAndUpdate, then update with { new: true } option.
B. Call findByIdAndUpdate directly without validation to save time.
C. Use findByIdAndUpdate with an empty update object if email is invalid.
D. Update the document without callback and ignore errors.
Solution
Step 1: Validate input before updating
Always check if the new email is valid and not empty before updating to avoid bad data.
Step 2: Use findByIdAndUpdate with { new: true }
Update the document with the valid email and use { new: true } to get the updated document.
Final Answer:
Check email validity before calling findByIdAndUpdate, then update with { new: true } option. -> Option A
Quick Check:
Validate input + update with new:true = A [OK]
Hint: Always validate input before updating documents [OK]