Arithmetic expressions help you do math with numbers stored in your database. They let you add, multiply, or divide values easily.
Arithmetic expressions ($add, $multiply, $divide) in MongoDB
{ $add: [expression1, expression2, ...] }
{ $multiply: [expression1, expression2, ...] }
{ $divide: [expression1, expression2] }Use arrays to list the values or fields you want to calculate.
$divide always takes exactly two values: numerator and denominator.
{ $add: [10, 5] }price by 2.{ $multiply: ["$price", 2] }{ $divide: [100, 4] }score1, score2, and score3 together.{ $add: ["$score1", "$score2", "$score3"] }This query calculates the total score by adding three subject scores, finds the average by dividing the total by 3, and doubles the math score for each student.
db.students.aggregate([
{
$project: {
name: 1,
totalScore: { $add: ["$math", "$science", "$english"] },
averageScore: { $divide: [ { $add: ["$math", "$science", "$english"] }, 3 ] },
doubleMath: { $multiply: ["$math", 2] }
}
}
])Make sure fields used in expressions exist and contain numbers to avoid errors.
You can combine these expressions to do more complex calculations.
Remember $divide requires exactly two arguments: numerator and denominator.
Arithmetic expressions let you do math inside MongoDB queries.
$add sums values, $multiply multiplies, and $divide divides two values.
Use them to calculate totals, averages, or adjust numbers stored in your data.
Practice
{ $add: [5, 3] } do?Solution
Step 1: Understand the $add operator
The $add operator sums the numbers inside the array.Step 2: Calculate the sum of 5 and 3
5 + 3 equals 8.Final Answer:
Adds 5 and 3 to get 8 -> Option BQuick Check:
$add: [5, 3] = 8 [OK]
- Confusing $add with $multiply
- Thinking $add subtracts values
- Assuming $add divides numbers
Solution
Step 1: Recall correct operator syntax
MongoDB arithmetic operators use $ followed by the operator name and an array of values.Step 2: Check each option
{ $multiply: [4, 7] } uses $multiply with an array [4, 7], which is correct syntax.Final Answer:
{ $multiply: [4, 7] } -> Option AQuick Check:
Correct syntax uses $multiply with array [OK]
- Omitting $ before multiply
- Using parentheses instead of brackets
- Passing values without array brackets
{ $divide: [ { $multiply: [10, 5] }, 2 ] }, what is the result?Solution
Step 1: Calculate the inner $multiply expression
$multiply: [10, 5] equals 50.Step 2: Divide the result by 2
$divide: [50, 2] equals 25.Final Answer:
25 -> Option AQuick Check:
(10 * 5) / 2 = 25 [OK]
- Dividing 10 by 5 instead of multiplying
- Forgetting to divide after multiplication
- Mixing order of operations
{ $divide: [10, { $add: 2, 3 }] }Solution
Step 1: Check $add operator usage
$add requires an array of values, but { $add: 2, 3 } uses separate arguments incorrectly.Step 2: Confirm correct syntax
The correct syntax is { $add: [2, 3] } with square brackets.Final Answer:
The $add operator must have an array of values, not separate arguments -> Option DQuick Check:
$add needs array syntax [OK]
- Using commas instead of array brackets
- Assuming $divide cannot divide numbers
- Passing numbers as strings
{ $add: ["$price1", "$price2", "$price3"] } and want to divide by 3?Solution
Step 1: Understand the average formula
Average = (sum of values) / number of values.Step 2: Check each option
{ $divide: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] } sums prices then divides by 3, matching the average formula.Final Answer:
{ $divide: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] } -> Option CQuick Check:
Sum prices then divide by count = average [OK]
- Dividing each price separately then adding
- Multiplying sum by count instead of dividing
- Dividing count by sum instead of sum by count
Ready to prove it?
Pass the test and this pattern moves unknown
