0
0
MongoDBquery~20 mins

$mul operator for multiplication in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $mul Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this update operation?
Given a MongoDB collection products with a document:
{ "_id": 1, "price": 100, "quantity": 5 }
What will be the document after running this update?
db.products.updateOne({ _id: 1 }, { $mul: { price: 1.1, quantity: 2 } })
A{ "_id": 1, "price": 110, "quantity": 7 }
B{ "_id": 1, "price": 110, "quantity": 10 }
C{ "_id": 1, "price": 101, "quantity": 7 }
D{ "_id": 1, "price": 100, "quantity": 10 }
Attempts:
2 left
💡 Hint
The $mul operator multiplies the current field value by the given number.
🧠 Conceptual
intermediate
1:30remaining
Which statement about $mul is true?
Choose the correct statement about the MongoDB $mul operator.
A$mul adds the multiplier value to the current field value.
B$mul can multiply any field type including strings and arrays.
C$mul replaces the field value with the multiplier instead of multiplying.
D$mul can only multiply numeric fields and will error if used on non-numeric fields.
Attempts:
2 left
💡 Hint
Think about what happens if you try to multiply a string by a number.
📝 Syntax
advanced
1:30remaining
Which update command correctly multiplies the field 'score' by 3?
Select the valid MongoDB update command that multiplies the 'score' field by 3 for documents with {_id: 5}.
Adb.collection.updateOne({ _id: 5 }, { $mul: { score: 3 } })
Bdb.collection.updateOne({ _id: 5 }, { $mul: { score: null } })
Cdb.collection.updateOne({ _id: 5 }, { $mul: { score: [3] } })
Ddb.collection.updateOne({ _id: 5 }, { $mul: { score: "3" } })
Attempts:
2 left
💡 Hint
The multiplier must be a number, not a string or other type.
🔧 Debug
advanced
2:00remaining
Why does this update fail?
Given a document:
{ "_id": 10, "count": "5" }
Why does this update fail?
db.collection.updateOne({ _id: 10 }, { $mul: { count: 2 } })
ABecause 'count' is a string, $mul cannot multiply it and throws an error.
BBecause the multiplier 2 is not a string.
CBecause the document does not have a numeric _id.
DBecause $mul requires the field to be missing to create it.
Attempts:
2 left
💡 Hint
Check the data type of the field being multiplied.
optimization
expert
2:30remaining
How to efficiently multiply multiple fields in many documents?
You want to multiply the fields 'price' and 'tax' by 1.05 in all documents of a large collection. Which approach is best for performance?
AFetch all documents, multiply fields in application code, then update each document individually.
BRun two separate updateMany commands, one for 'price' and one for 'tax'.
CRun a single updateMany with $mul on both fields: db.collection.updateMany({}, { $mul: { price: 1.05, tax: 1.05 } })
DUse $inc operator to add 5% of the current value to each field.
Attempts:
2 left
💡 Hint
Consider how many database operations are needed and atomicity.