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?