0% completed
Arithmetic operators in MongoDB are used to perform mathematical operations on numerical values within documents. These operators enable you to perform addition, subtraction, multiplication, division, and more, directly within your queries and update operations.
Understanding how to use arithmetic operators effectively can help you manipulate and transform data as needed for your applications.
Operator | Description |
---|---|
$abs | Returns the absolute value of a number. |
$add | Adds numbers to return the sum, or adds numbers and a date to return a new date. |
$ceil | Returns the smallest integer greater than or equal to the specified number. |
$divide | Divides the first number by the second. |
$exp | Raises e to the specified exponent. |
$floor | Returns the largest integer less than or equal to the specified number. |
$ln | Calculates the natural logarithm of a number. |
$log | Calculates the logarithm of a number in the specified base. |
$log10 | Calculates the logarithm base 10 of a number. |
$mod | Returns the remainder of the first number divided by the second. |
$multiply | Multiplies numbers to return the product. |
$pow | Raises a number to the specified exponent. |
$round | Rounds a number to a whole integer or to a specified decimal place. |
$sqrt | Calculates the square root of a number. |
$subtract | Subtracts the second value from the first. |
$trunc | Truncates a number to a whole integer or to a specified decimal place. |
The general syntax for using arithmetic operators in MongoDB is:
db.collection.aggregate([ { $project: { <field>: { <operator>: [ <expression1>, <expression2>, ... ] } } } ])
Here, the aggregate()
method is used to update the array data. However, you can also use other methods with an array to perform different operations.
First, let's insert some documents into the products
collection to work with:
db.products.insertMany([ { name: "Product A", price: 10, quantity: 100 }, { name: "Product B", price: 20, quantity: 150 }, { name: "Product C", price: 15, quantity: 200 }, { name: "Product D", price: 25, quantity: 50 } ])
This setup creates a products
collection with various documents containing name
, price
, and quantity
fields. We will use these documents for demonstrating the arithmetic operators in the subsequent sections.
The $add
operator adds numbers together.
Add Price and Quantity
db.products.aggregate([ { $project: { name: 1, totalValue: { $add: ["$price", "$quantity"] } } } ])
This command calculates the total value by adding the price
and quantity
fields for each product.
Add Constant Value to Price
db.products.aggregate([ { $project: { name: 1, increasedPrice: { $add: ["$price", 5] } } } ])
This command calculates the increased price by adding 5 to the price
field for each product.
The $subtract
operator subtracts the second number from the first.
Subtract Quantity from Price
db.products.aggregate([ { $project: { name: 1, difference: { $subtract: ["$price", "$quantity"] } } } ])
This command calculates the difference by subtracting the quantity
field from the price
field for each product.
Subtract Constant Value from Price
db.products.aggregate([ { $project: { name: 1, discountedPrice: { $subtract: ["$price", 5] } } } ])
This command calculates the discounted price by subtracting 5 from the price
field for each product.
The $multiply
operator multiplies numbers together.
Multiply Price and Quantity
db.products.aggregate([ { $project: { name: 1, totalValue: { $multiply: ["$price", "$quantity"] } } } ])
This command calculates the total value by multiplying the price
and quantity
fields for each product.
Multiply Price by Constant Value
db.products.aggregate([ { $project: { name: 1, doubledPrice: { $multiply: ["$price", 2] } } } ])
This command calculates the doubled price by multiplying the price
field by 2 for each product.
The $divide
operator divides the first number by the second.
Divide Quantity by Price
db.products.aggregate([ { $project: { name: 1, ratio: { $divide: ["$quantity", "$price"] } } } ])
This command calculates the ratio by dividing the quantity
field by the price
field for each product.
Divide Price by Constant Value
db.products.aggregate([ { $project: { name: 1, halfPrice: { $divide: ["$price", 2] } } } ])
This command calculates the half price by dividing the price
field by 2 for each product.
The $mod
operator calculates the remainder of the division of the first number by the second.
Calculate Remainder of Quantity Divided by Price
db.products.aggregate([ { $project: { name: 1, remainder: { $mod: ["$quantity", "$price"] } } } ])
This command calculates the remainder by dividing the quantity
field by the price
field for each product.
Calculate Remainder of Price Divided by Constant Value
db.products.aggregate([ { $project: { name: 1, remainder: { $mod: ["$price", 7] } } } ])
This command calculates the remainder by dividing the price
field by 7 for each product.
.....
.....
.....