Learning MongoDB

0% completed

Previous
Next
Arithmetic Operators

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.

Arithmetic Operators

OperatorDescription
$absReturns the absolute value of a number.
$addAdds numbers to return the sum, or adds numbers and a date to return a new date.
$ceilReturns the smallest integer greater than or equal to the specified number.
$divideDivides the first number by the second.
$expRaises e to the specified exponent.
$floorReturns the largest integer less than or equal to the specified number.
$lnCalculates the natural logarithm of a number.
$logCalculates the logarithm of a number in the specified base.
$log10Calculates the logarithm base 10 of a number.
$modReturns the remainder of the first number divided by the second.
$multiplyMultiplies numbers to return the product.
$powRaises a number to the specified exponent.
$roundRounds a number to a whole integer or to a specified decimal place.
$sqrtCalculates the square root of a number.
$subtractSubtracts the second value from the first.
$truncTruncates a number to a whole integer or to a specified decimal place.

Syntax for Using Arithmetic Operators

The general syntax for using arithmetic operators in MongoDB is:

db.collection.aggregate([ { $project: { <field>: { <operator>: [ <expression1>, <expression2>, ... ] } } } ])
  • field: The field to project.
  • operator: The arithmetic operator to apply.
  • expression1, expression2, ...: The numerical expressions to operate on.

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.

Example Setup

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.

$add Operator

The $add operator adds numbers together.

Examples

  1. 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.

  2. 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.

$subtract Operator

The $subtract operator subtracts the second number from the first.

Examples

  1. 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.

  2. 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.

$multiply Operator

The $multiply operator multiplies numbers together.

Examples

  1. 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.

  2. 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.

$divide Operator

The $divide operator divides the first number by the second.

Examples

  1. 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.

  2. 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.

$mod Operator

The $mod operator calculates the remainder of the division of the first number by the second.

Examples

  1. 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.

  2. 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.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next