Complete the code to add two fields 'price' and 'tax' in a MongoDB aggregation pipeline.
{ $project: { totalCost: { $add: ["$price", [1] ] } } }In MongoDB aggregation, to refer to a field, you use "$" before the field name. So "$tax" correctly refers to the 'tax' field.
Complete the code to multiply 'quantity' by 'price' to get 'total' in a MongoDB aggregation pipeline.
{ $project: { total: { $multiply: ["$quantity", [1] ] } } }Use "$price" to correctly reference the 'price' field in the aggregation pipeline.
Fix the error in the code to divide 'total' by 'count' in a MongoDB aggregation pipeline.
{ $project: { average: { $divide: ["[1]", "$count" ] } } }To refer to a field inside an aggregation expression, you must use "$" before the field name and wrap it in quotes.
Fill both blanks to calculate the total price including tax by adding 'price' and the product of 'price' and 'taxRate'.
{ $project: { totalPrice: { $add: ["$price", { $multiply: ["$price", [1] ] } ] } } }To multiply 'price' by 'taxRate', both fields must be referenced with a $ prefix inside strings.
Fill all three blanks to calculate the average price per item after discount: subtract 'discount' from 'price', then divide by 'quantity'.
{ $project: { avgPrice: { $divide: [ { $subtract: ["[1]", "[2]" ] }, "[3]" ] } } }Each field must be referenced with a $ prefix inside quotes to be correctly used in aggregation expressions.