A. Cannot multiply a field by a number in $addFields
B. Incorrect use of $multiply operator syntax; expects an array
C. Field name discountPrice is invalid
D. Missing $ before price
Solution
Step 1: Check $multiply syntax
$multiply requires an array of expressions, but here it is given two separate arguments.
Step 2: Identify correct syntax
The correct syntax is { $multiply: ["$price", 0.9] } using an array.
Final Answer:
Incorrect use of $multiply operator syntax; expects an array -> Option B
Quick Check:
$multiply needs array of operands [OK]
Hint: Always use array syntax with $multiply [OK]
Common Mistakes:
Passing arguments to $multiply without array brackets
Assuming field names cannot be multiplied by numbers
Confusing field name syntax with missing $
5. You have documents with fields price and taxRate. You want to add a field finalPrice that is price plus price multiplied by taxRate. Which $addFields stage correctly computes this?
The formula is price + (price * taxRate), which requires addition and multiplication.
Step 2: Check correct MongoDB operators usage
{ $addFields: { finalPrice: { $add: ["$price", { $multiply: ["$price", "$taxRate"] }] } } } uses $add with an array containing price and the multiplication result of price and taxRate, which is correct syntax.