0
0
MongoDBquery~30 mins

updateMany method in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the updateMany Method in MongoDB
📖 Scenario: You manage a small online bookstore database. You want to update the prices of all books in a specific category to offer a discount.
🎯 Goal: Learn how to use the updateMany method in MongoDB to update multiple documents at once based on a condition.
📋 What You'll Learn
Create a collection named books with sample book documents.
Define a filter to select books in the 'Science Fiction' category.
Use updateMany to reduce the price of all selected books by 10%.
Verify the update operation is correctly structured.
💡 Why This Matters
🌍 Real World
Updating prices or statuses of multiple products or records in a database at once is common in e-commerce and inventory management.
💼 Career
Knowing how to use updateMany helps database administrators and backend developers efficiently manage bulk updates in MongoDB.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array of three book documents with these exact fields and values: { title: 'Dune', category: 'Science Fiction', price: 20 }, { title: '1984', category: 'Science Fiction', price: 15 }, and { title: 'Pride and Prejudice', category: 'Romance', price: 10 }.
MongoDB
Need a hint?

Remember to create an array with three objects exactly as described.

2
Define the filter for books in the 'Science Fiction' category
Create a variable called filter and set it to an object that selects documents where the category field is exactly 'Science Fiction'.
MongoDB
Need a hint?

Use an object with the key category and value 'Science Fiction'.

3
Write the update operation to reduce prices by 10%
Create a variable called update and set it to an object that uses the $mul operator to multiply the price field by 0.9 (to reduce price by 10%).
MongoDB
Need a hint?

Use the $mul operator inside the update object to multiply the price.

4
Use updateMany to apply the price reduction
Write a line calling db.books.updateMany(filter, update); to update all books matching the filter with the price reduction.
MongoDB
Need a hint?

Use the updateMany method on db.books with the filter and update variables.