Nearby lessons

15 of 22

🧮 MongoDB: Arithmetic Operators

MongoDB provides a range of arithmetic operators that allow you to perform mathematical calculations directly within aggregation pipelines. These operators help in computing totals, differences, percentages, averages, and other numeric transformations efficiently.

📚 Initial Data Setup — sales Collection

MongoDB Shell
NoSQL
Database
1db.sales.insertMany([
2 { _id: 1, product: 'Mobile', price: 100, quantity: 52 },
3 { _id: 2, product: 'Laptop', price: 150, quantity: 65 },
4 { _id: 3, product: 'Tablet', price: 120, quantity: 30 }
5]);
Query Result
JSON Output
✅ 3 sales documents inserted successfully.

1️⃣ Using $add — Add Values

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $add: ["$price", "$quantity"] } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 152 },
{ "product": "Laptop", "result": 215 },
{ "product": "Tablet", "result": 150 }
]

2️⃣ Using $subtract — Subtract Values

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $subtract: ["$price", "$quantity"] } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 48 },
{ "product": "Laptop", "result": 85 },
{ "product": "Tablet", "result": 90 }
]

3️⃣ Using $multiply — Multiply Values

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $multiply: ["$price", "$quantity"] } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 5200 },
{ "product": "Laptop", "result": 9750 },
{ "product": "Tablet", "result": 3600 }
]

4️⃣ Using $divide — Divide Values

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $divide: ["$price", "$quantity"] } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 1.9230769230769231 },
{ "product": "Laptop", "result": 2.3076923076923075 },
{ "product": "Tablet", "result": 4 }
]

5️⃣ Using $mod — Modulus (Remainder)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $mod: ["$quantity", 2] } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 0 },
{ "product": "Laptop", "result": 1 },
{ "product": "Tablet", "result": 0 }
]

6️⃣ Using $pow — Exponentiation (price²)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $pow: ["$price", 2] } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 10000 },
{ "product": "Laptop", "result": 22500 },
{ "product": "Tablet", "result": 14400 }
]

7️⃣ Using $round — Round Result (price / 3)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $round: [{ $divide: ["$price", 3] }, 0] } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 33 },
{ "product": "Laptop", "result": 50 },
{ "product": "Tablet", "result": 40 }
]

8️⃣ Using $sqrt — Square Root (√price)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $sqrt: "$price" } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 10 },
{ "product": "Laptop", "result": 12.24744871391589 },
{ "product": "Tablet", "result": 10.954451150103322 }
]

9️⃣ Using $abs — Absolute Value (|price - 200|)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $abs: { $subtract: ["$price", 200] } } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 100 },
{ "product": "Laptop", "result": 50 },
{ "product": "Tablet", "result": 80 }
]

🔟 Using $ceil — Round Up (quantity / 4)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $ceil: { $divide: ["$quantity", 4] } } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 13 },
{ "product": "Laptop", "result": 17 },
{ "product": "Tablet", "result": 8 }
]

1️⃣1️⃣ Using $floor — Round Down (quantity / 4)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $floor: { $divide: ["$quantity", 4] } } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 13 },
{ "product": "Laptop", "result": 16 },
{ "product": "Tablet", "result": 7 }
]

1️⃣2️⃣ Using $trunc — Truncate Value (quantity / 4)

MongoDB Shell
NoSQL
Database
1db.sales.aggregate([
2 { $project: { product: 1, result: { $trunc: { $divide: ["$quantity", 4] } } } }
3]);
Query Result
JSON Output
[
{ "product": "Mobile", "result": 13 },
{ "product": "Laptop", "result": 16 },
{ "product": "Tablet", "result": 7 }
]
Keep Going!MongoDB - String Operators