Bird
Raised Fist0
MongoDBquery~30 mins

Arithmetic expressions ($add, $multiply, $divide) in MongoDB - Mini Project: Build & Apply

Choose your learning style10 modes available
Using Arithmetic Expressions in MongoDB Aggregation
📖 Scenario: You work in a small store that keeps product sales data in a MongoDB collection. You want to calculate the total revenue for each product by multiplying the quantity sold by the price per unit. You also want to add a fixed tax amount to the total revenue and then divide the final amount by 2 to find the average revenue over two months.
🎯 Goal: Build a MongoDB aggregation pipeline that uses the arithmetic expressions $add, $multiply, and $divide to calculate the average revenue including tax for each product.
📋 What You'll Learn
Create a collection named sales with documents containing product, quantity, and price fields.
Add a fixed tax amount variable named tax with the value 5.
Use $multiply to calculate total revenue by multiplying quantity and price.
Use $add to add the tax to the total revenue.
Use $divide to divide the sum by 2 to get the average revenue.
Output the product name and the calculated average revenue as averageRevenue.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to calculate revenues, taxes, and averages from sales data stored in databases like MongoDB.
💼 Career
Understanding MongoDB aggregation and arithmetic expressions is essential for data analysts and backend developers working with NoSQL databases.
Progress0 / 4 steps
1
Create the sales collection with product data
Create a MongoDB collection named sales with these exact documents: { product: "Pen", quantity: 10, price: 2 }, { product: "Notebook", quantity: 5, price: 4 }, and { product: "Eraser", quantity: 20, price: 1 }.
MongoDB
Hint

Use db.sales.insertMany([...]) to add multiple documents at once.

2
Add a tax variable for the fixed tax amount
Create a variable named tax and set it to the number 5 to represent the fixed tax amount.
MongoDB
Hint

Use const tax = 5 to create a fixed tax variable.

3
Calculate total revenue and add tax using $multiply and $add
Write a MongoDB aggregation pipeline that uses $multiply to multiply quantity and price to get total revenue, then uses $add to add the tax variable to this total. Use $project to output product and the calculated totalWithTax.
MongoDB
Hint

Use $project to create a new field totalWithTax that adds tax to the product of quantity and price.

4
Divide the total with tax by 2 using $divide to get average revenue
Extend the aggregation pipeline to use $divide to divide totalWithTax by 2. Output the product and the result as averageRevenue.
MongoDB
Hint

Use $divide inside $project to divide the sum by 2 and rename the field to averageRevenue.

Practice

(1/5)
1. What does the MongoDB expression { $add: [5, 3] } do?
easy
A. Multiplies 5 and 3 to get 15
B. Adds 5 and 3 to get 8
C. Divides 5 by 3 to get 1.67
D. Subtracts 3 from 5 to get 2

Solution

  1. Step 1: Understand the $add operator

    The $add operator sums the numbers inside the array.
  2. Step 2: Calculate the sum of 5 and 3

    5 + 3 equals 8.
  3. Final Answer:

    Adds 5 and 3 to get 8 -> Option B
  4. Quick Check:

    $add: [5, 3] = 8 [OK]
Hint: Remember $add sums all values inside the array [OK]
Common Mistakes:
  • Confusing $add with $multiply
  • Thinking $add subtracts values
  • Assuming $add divides numbers
2. Which of the following is the correct syntax to multiply 4 by 7 using MongoDB arithmetic expressions?
easy
A. { $multiply: [4, 7] }
B. { $multiply: 4, 7 }
C. { $multiply: (4, 7) }
D. { multiply: [4, 7] }

Solution

  1. Step 1: Recall correct operator syntax

    MongoDB arithmetic operators use $ followed by the operator name and an array of values.
  2. Step 2: Check each option

    { $multiply: [4, 7] } uses $multiply with an array [4, 7], which is correct syntax.
  3. Final Answer:

    { $multiply: [4, 7] } -> Option A
  4. Quick Check:

    Correct syntax uses $multiply with array [OK]
Hint: Use square brackets [] for values in $multiply [OK]
Common Mistakes:
  • Omitting $ before multiply
  • Using parentheses instead of brackets
  • Passing values without array brackets
3. Given the MongoDB aggregation expression { $divide: [ { $multiply: [10, 5] }, 2 ] }, what is the result?
medium
A. 25
B. 100
C. 2.5
D. 50

Solution

  1. Step 1: Calculate the inner $multiply expression

    $multiply: [10, 5] equals 50.
  2. Step 2: Divide the result by 2

    $divide: [50, 2] equals 25.
  3. Final Answer:

    25 -> Option A
  4. Quick Check:

    (10 * 5) / 2 = 25 [OK]
Hint: Calculate inner expressions first, then outer [OK]
Common Mistakes:
  • Dividing 10 by 5 instead of multiplying
  • Forgetting to divide after multiplication
  • Mixing order of operations
4. Identify the error in this MongoDB expression: { $divide: [10, { $add: 2, 3 }] }
medium
A. There is no error; the expression is correct
B. The $divide operator cannot divide numbers
C. The numbers 10, 2, and 3 must be strings
D. The $add operator must have an array of values, not separate arguments

Solution

  1. Step 1: Check $add operator usage

    $add requires an array of values, but { $add: 2, 3 } uses separate arguments incorrectly.
  2. Step 2: Confirm correct syntax

    The correct syntax is { $add: [2, 3] } with square brackets.
  3. Final Answer:

    The $add operator must have an array of values, not separate arguments -> Option D
  4. Quick Check:

    $add needs array syntax [OK]
Hint: Always use square brackets [] for $add values [OK]
Common Mistakes:
  • Using commas instead of array brackets
  • Assuming $divide cannot divide numbers
  • Passing numbers as strings
5. You want to calculate the average price of items stored in a MongoDB collection using aggregation. Which expression correctly calculates the average price if you have total price as { $add: ["$price1", "$price2", "$price3"] } and want to divide by 3?
hard
A. { $multiply: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] }
B. { $add: [ { $divide: ["$price1", 3] }, { $divide: ["$price2", 3] }, { $divide: ["$price3", 3] } ] }
C. { $divide: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] }
D. { $divide: [ 3, { $add: ["$price1", "$price2", "$price3"] } ] }

Solution

  1. Step 1: Understand the average formula

    Average = (sum of values) / number of values.
  2. Step 2: Check each option

    { $divide: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] } sums prices then divides by 3, matching the average formula.
  3. Final Answer:

    { $divide: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] } -> Option C
  4. Quick Check:

    Sum prices then divide by count = average [OK]
Hint: Sum first, then divide by count for average [OK]
Common Mistakes:
  • Dividing each price separately then adding
  • Multiplying sum by count instead of dividing
  • Dividing count by sum instead of sum by count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard