📖 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
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
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
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
Hint
Use db.products.find() to see all documents in the collection.
Practice
(1/5)
1. What does the $mul operator do in MongoDB?
easy
A. It multiplies the value of a field by a specified number.
B. It adds a specified number to the value of a field.
C. It divides the value of a field by a specified number.
D. It subtracts a specified number from the value of a field.
Solution
Step 1: Understand the purpose of $mul
The $mul operator is used to multiply the value of a numeric field by a given number in an update operation.
Step 2: Compare with other arithmetic operators
Unlike $inc which adds, or $div which divides, $mul specifically multiplies the field value.
Final Answer:
It multiplies the value of a field by a specified number. -> Option A
Quick Check:
$mul = multiply [OK]
Hint: Remember: $mul means multiply field by number [OK]
Common Mistakes:
Confusing $mul with $inc (addition)
Thinking $mul divides instead of multiplies
Assuming $mul works on non-numeric fields
2. Which of the following is the correct syntax to multiply the field price by 3 in a MongoDB update?
easy
A. { $mul: { price: price * 3 } }
B. { $mul: { 3: price } }
C. { $mul: { price: "3" } }
D. { $mul: { price: 3 } }
Solution
Step 1: Identify correct operator usage
The $mul operator requires a field name as key and a numeric multiplier as value.
Hint: Use field name as key and number as value inside $mul [OK]
Common Mistakes:
Using string instead of number for multiplier
Swapping key and value inside $mul
Trying to use expressions inside $mul
3. Given a document { _id: 1, quantity: 5, price: 10 }, what will be the document after running db.collection.updateOne({ _id: 1 }, { $mul: { quantity: 2, price: 3 } })?
medium
A. { _id: 1, quantity: 2, price: 3 }
B. { _id: 1, quantity: 10, price: 30 }
C. { _id: 1, quantity: 5, price: 10 }
D. { _id: 1, quantity: 7, price: 13 }
Solution
Step 1: Understand the update operation
The $mul operator multiplies each specified field by the given number. Here, quantity is multiplied by 2 and price by 3.
Step 2: Calculate new field values
Original quantity 5 * 2 = 10, original price 10 * 3 = 30.
Final Answer:
{ _id: 1, quantity: 10, price: 30 } -> Option B
Quick Check:
5*2=10 and 10*3=30 [OK]
Hint: Multiply each field value by given number in $mul [OK]
Common Mistakes:
Adding instead of multiplying
Not updating all specified fields
Confusing field names or values
4. You run this update: db.products.updateOne({ _id: 2 }, { $mul: { stock: '2' } }). What is the issue?
medium
A. The update query is missing $set operator.
B. The field name 'stock' is invalid.
C. The multiplier '2' is a string, should be a number.
D. The filter {_id: 2} is incorrect.
Solution
Step 1: Check the multiplier type
The $mul operator requires the multiplier to be a number, not a string.
Step 2: Identify the error in the update
Here, '2' is a string (in quotes), which causes a type error in MongoDB.
Final Answer:
The multiplier '2' is a string, should be a number. -> Option C
Quick Check:
Multiplier must be numeric, not string [OK]
Hint: Multiplier must be a number, not a string [OK]
Common Mistakes:
Using quotes around numbers in $mul
Assuming $set is needed with $mul
Blaming filter instead of multiplier type
5. You want to double the score field only if it is greater than 10 in documents. Which update query correctly uses $mul with a filter?