Introduction to MongoDB $subtract Operator
The $subtract operator is a mathematical operator in MongoDB that calculates the difference between two numeric values and returns the result. This operator is commonly used in the aggregation pipeline.
Syntax
The syntax of the $subtract operator is as follows:
{ $subtract: [ <expression1>, <expression2> ] }
Here, <expression1> and <expression2> are both parameters that represent a number or a computable expression. The operator returns the result of <expression1> minus <expression2>.
Use Cases
The $subtract operator is commonly used in the aggregation pipeline to calculate the difference between certain fields and create new fields, or to calculate the result of an expression in the $project stage. For example, $subtract can be used to calculate the difference between the purchase quantity and the return quantity in a sales order, or to calculate the number of days between a date field and the current date.
Examples
Here are two examples of the $subtract operator.
Example Data
Consider the following example data:
{
"_id": 1,
"price": 10,
"cost": 5
},
{
"_id": 2,
"price": 15,
"cost": 8
},
{
"_id": 3,
"price": 20,
"cost": 10
}
Example 1: Calculating the Cost Difference
Suppose we want to calculate the cost difference (cost_diff) for each document, i.e., the difference between the cost and 5. We can use the $subtract operator to achieve this:
db.products.aggregate([
{
$project: {
_id: 1,
price: 1,
cost: 1,
cost_diff: { $subtract: ["$cost", 5] }
}
}
])
This aggregation pipeline will output the following documents:
{
"_id": 1,
"price": 10,
"cost": 5,
"cost_diff": 0
},
{
"_id": 2,
"price": 15,
"cost": 8,
"cost_diff": 3
},
{
"_id": 3,
"price": 20,
"cost": 10,
"cost_diff": 5
}Example 2: Calculating Date Time Difference
Suppose we have a collection containing order dates and delivery dates, and we want to calculate the delivery time in milliseconds for each order. We can use the $subtract operator to calculate the time difference:
db.orders.aggregate([
{
$project: {
_id: 1,
order_date: 1,
delivery_date: 1,
delivery_time: { $subtract: ["$delivery_date", "$order_date"] }
}
}
])
This aggregation pipeline will output the following documents:
{
"_id": 1,
"order_date": ISODate("2022-02-01T00:00:00Z"),
"delivery_date": ISODate("2022-02-05T00:00:00Z"),
"delivery_time": 345600000
},
{
"_id": 2,
"order_date": ISODate("2022-03-01T00:00:00Z"),
"delivery_date": ISODate("2022-03-03T00:00:00Z"),
"delivery_time": 172800000
}Example 3: Calculate the difference between two fields and create a new field
Assume we have the following sales collection:
{ _id: 1, item: "apple", quantity: 10, returned: 2 }
{ _id: 2, item: "banana", quantity: 20, returned: 5 }
{ _id: 3, item: "pear", quantity: 15, returned: 3 }
We can use the $subtract operator to calculate the difference between quantity and returned, and create a new field called net_quantity:
db.sales.aggregate([
{
$project: {
_id: 1,
item: 1,
net_quantity: { $subtract: ["$quantity", "$returned"] }
}
}
])
After executing the above aggregation pipeline, we will get the following result:
{ "_id": 1, "item": "apple", "net_quantity": 8 }
{ "_id": 2, "item": "banana", "net_quantity": 15 }
{ "_id": 3, "item": "pear", "net_quantity": 12 }Conclusion
The $subtract operator is a mathematical operator in MongoDB used to calculate the difference between two numeric values and return the result. It is commonly used in aggregation pipelines.