$divide
The $divide operator divides one number by another.
📌 Syntax
{ "$divide": [ <numerator>, <denominator> ] }
Both arguments must resolve to numeric values. Division by zero will result in an error or null.
✅ Base Example 1 – Simple Division
📥 Input Document
{ "total": 100, "parts": 4 }
📌 Expression
{ "$divide": ["$total", "$parts"] }
📤 Output
25
✅ Base Example 2 – Convert Cents to Dollars
📥 Input Document
{ "amountInCents": 1250 }
📌 Expression
{ "$divide": ["$amountInCents", 100] }
📤 Output
12.5
🧱 Ecommerce Example – Compute Unit Price per Item
📌 Pipeline
[
{ "$unwind": "$items" },
{
"$project": {
"product": "$items.name",
"unitPrice": {
"$divide": ["$items.totalPrice", "$items.quantity"]
}
}
}
]
📥 Input Document
{
"orderId": 2002,
"items": [
{ "name": "Chair", "totalPrice": 300, "quantity": 3 },
{ "name": "Desk", "totalPrice": 500, "quantity": 1 }
]
}
📤 Output
[
{ "product": "Chair", "unitPrice": 100 },
{ "product": "Desk", "unitPrice": 500 }
]
🧱 Ecommerce Example – Discount Percent
📌 Expression
{ "$divide": ["$discount", "$price"] }
Used to compute the proportion of discount relative to the price.
📥 Input Document
{ "price": 250, "discount": 25 }
📤 Output
0.1
🔧 Common Use Cases
- Price per unit
- Percentage calculations
- Normalization
🔗 Related Operators
$add,$subtract,$multiply,$mod$cond,$round,$project
🧠Notes
- If denominator is
0, the result may benullor cause an error. - Always validate for zero if dynamic input is used.