0
0
MongoDBquery~30 mins

$mul operator for multiplication in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
$mul Operator for Multiplication in MongoDB
📖 Scenario: You are managing a small online store's inventory database. You want to update the prices of some products by multiplying their current prices by a factor to apply a discount or increase.
🎯 Goal: Learn how to use the $mul operator in MongoDB to multiply numeric fields in documents.
📋 What You'll Learn
Create a collection named products with specific product documents.
Define a multiplication factor variable.
Use the $mul operator in an update query to multiply the price field of all products.
Verify the update by querying the collection.
💡 Why This Matters
🌍 Real World
Online stores often need to update prices in bulk due to sales, inflation, or supplier changes. Using $mul helps automate these updates efficiently.
💼 Career
Database administrators and backend developers use update operators like $mul to maintain and manipulate data in production databases.
Progress0 / 4 steps
1
Create the products collection with initial data
Create a MongoDB collection called products and insert these exact documents: { _id: 1, name: "Pen", price: 10 }, { _id: 2, name: "Notebook", price: 20 }, and { _id: 3, name: "Eraser", price: 5 }.
MongoDB
Need a hint?

Use db.products.insertMany() with an array of objects to add multiple documents.

2
Define the multiplication factor
Create a variable called multiplier and set it to 1.1 to represent a 10% price increase.
MongoDB
Need a hint?

Use const multiplier = 1.1 to define the multiplication factor.

3
Update all product prices using the $mul operator
Write an update query using db.products.updateMany() to multiply the price field of all documents by the multiplier variable using the $mul operator.
MongoDB
Need a hint?

Use db.products.updateMany({}, { $mul: { price: multiplier } }) to multiply prices.

4
Verify the updated prices
Write a query using db.products.find() to retrieve all documents and check that the price fields have been updated correctly.
MongoDB
Need a hint?

Use db.products.find() to see all documents in the collection.