Javatpoint Logo
Javatpoint Logo

MongoDB $multiply Operator

What is the $multiply operator in MongoDB?

MongoDB provides a variety of arithmetic expression operators. The $multiply operator is one of those operators. This operator is used in the aggregation pipeline stages. This operator is used to multiply two or more numbers and return the result.

Syntax of the $multiply operator:

To use this operator, you have to pass the arguments in an array.

Examples

In the following examples, we are working with:

>db.products.find().pretty()
{
    {
        "_id" : 1,
        "name" : "BlueBox",
        "x" : 10,
        "y" : 50,
        "billYear" : 2018
    }
    {
        "_id" : 2,
        "name" : "GreenBox",
        "x" : 10,
        "y" : 6,
        "billYear" : 2017
    }
    {
        "_id" : 3,
        "name" : "RedBox",
        "x" : 7,
        "y" : 9,
        "billYear" : 2018
    }
    {
        "_id" : 4,
        "name" : "WhiteBox",
        "x" : 2,
        "y" : 7,
        "z" : 4,
        "billYear" : 2019
    }
    {
        "_id" : 5,
        "name" : "BlueBox",
        "x" : 4,
        "y" : 12,
        "billYear" : 2020
    }
    {
        "_id" : 6,
        "name" : "BlueBox",
        "x" : 10,
        "y" : 5,
        "billYear" : 2021
    }
    {
        "_id" : 7,
        "name" : "WhiteBox",
        "x" : 5,
        "y" : 1,
        "z" : 45,
        "billYear" : 2019
    }
    {
        "_id" : 8,
        "name" : "GreenBox",
        "x" : -15,
        "y" : 5,
        "billYear" : 2018
    }
    {
        "_id" : 9,
        "name" : "BlackBox",
        "billDetail" : {
                               "x" : 45,
                               "y" : 56,
                              }
        "billYear" : 2021
    }
    {
        "_id" : 10,
        "name" : "WhiteBox",
        "x" : 4,
        "y" : 5,
        "z" : 6,
        "billYear" : 2020
    }
}

Example 1: Using $multiply operator (for two numbers)

In this example, we're going to multiply the "x" and "y" fields in the BlueBox.

Output:

{
        "_id" : 1,
        "name" : "BlueBox",
        "x" : 10,
        "y" : 50,
        "answer" : 500,
}
{
        "_id" : 5,
        "name" : "BlueBox",
        "x" : 4,
        "y" : 12,
        "answer" : 48,
}
{
        "_id" : 6,
        "name" : "BlueBox",
        "x" : 10,
        "y" : 5,
        "answer" : 50,
}

Example 2: Using $multiply operator (for three numbers)

In this example, we're going to multiply the "x", "y", and "z" fields together in the WhiteBox.

Output:

{
        "_id" : 4,
        "name" : "WhiteBox",
        "x" : 2,
        "y" : 7,
        "z" : 4,
        "answer" : 56
}
{
        "_id" : 7,
        "name" : "WhiteBox",
        "x" : 5,
        "y" : 1,
        "z" : 45,
        "answer" : 225
}
{
        "_id" : 10,
        "name" : "WhiteBox",
        "x" : 4,
        "y" : 5,
        "z" : 6,
        "answer" : 120
}

Example 3: Using $multiply operator in the embedded document

In this example, we're going to multiply the "billDetail.x", and "billDetail.y" fields in the BlackBox.

Output:

{
        "_id" : 9,
        "name" : "BlackBox",
        "x" : 45,
        "y" : 56,
        "answer" : 2520
}

Example 4: Add a fixed number to the array

If the user wants to multiply the field by a certain number, the user can do so. In this example, we're going to multiple the "x" field by 10 in the RedBox.

Output:

{
        "_id" : 3,
        "name" : "RedBox",
        "x" : 7,
        "answer" : 70,
}    






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA