0
0
MongoDBquery~10 mins

Arithmetic expressions ($add, $multiply, $divide) in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add two fields 'price' and 'tax' in a MongoDB aggregation pipeline.

MongoDB
{ $project: { totalCost: { $add: ["$price", [1] ] } } }
Drag options to blanks, or click blank then click option'
A"$tax"
B"tax"
C"$total"
D"cost"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the $ before the field name
Using the field name as a string without $
2fill in blank
medium

Complete the code to multiply 'quantity' by 'price' to get 'total' in a MongoDB aggregation pipeline.

MongoDB
{ $project: { total: { $multiply: ["$quantity", [1] ] } } }
Drag options to blanks, or click blank then click option'
A"price"
B"$price"
C"cost"
D"amount"
Attempts:
3 left
💡 Hint
Common Mistakes
Using field names without $
Using incorrect field names
3fill in blank
hard

Fix the error in the code to divide 'total' by 'count' in a MongoDB aggregation pipeline.

MongoDB
{ $project: { average: { $divide: ["[1]", "$count" ] } } }
Drag options to blanks, or click blank then click option'
A"$total"
B"total"
C$total
Dtotal
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted field names
Omitting the $ prefix
4fill in blank
hard

Fill both blanks to calculate the total price including tax by adding 'price' and the product of 'price' and 'taxRate'.

MongoDB
{ $project: { totalPrice: { $add: ["$price", { $multiply: ["$price", [1] ] } ] } } }
Drag options to blanks, or click blank then click option'
A"$tax"
B"taxRate"
C"$taxRate"
D"tax"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using $ before 'taxRate'
Using incorrect field names
5fill in blank
hard

Fill all three blanks to calculate the average price per item after discount: subtract 'discount' from 'price', then divide by 'quantity'.

MongoDB
{ $project: { avgPrice: { $divide: [ { $subtract: ["[1]", "[2]" ] }, "[3]" ] } } }
Drag options to blanks, or click blank then click option'
A"$price"
B"$discount"
C"$quantity"
D"price"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting $ before field names
Using field names without quotes