Javatpoint Logo
Javatpoint Logo

MongoDB $divide Operator

What is the $divide operator in MongoDB?

MongoDB provides a variety of arithmetic expression operators. The $divide operator is one of those operators. This operator is used in the aggregation pipeline stages. It is also used to divide one number by another and return the result.

Syntax of the $divide operator:

To use this operator, you have to pass the arguments in an array. In this operator, the first argument is a dividend, and the second argument is a divisor.

Examples:

In the following examples, we are working with:

Output:

>db.products.find().pretty()
{
    {
        "_id" : 1,
        "name" : "Mobile",
        "totalPrice" : 100000,
        "totalQuantity" : 50,
        "billYear" : 2018
    }
    {
        "_id" : 2,
        "name" : "Keyboard",
        "totalPrice" : 5000,
        "totalQuantity" : 10,
        "billYear" : 2017
    }
    {
        "_id" : 3,
        "name" : "Mouse",
        "totalPrice" : 2000,
        "totalQuantity" : 5,
        "billYear" : 2018
    }
    {
        "_id" : 4,
        "name" : "Memory Card",
        "totalPrice" : 2500,
        "totalQuantity" : 25,
        "billYear" : 2019
    }
    {
        "_id" : 5,
        "name" : "Mobile",
        "totalPrice" : 20000,
        "totalQuantity" : 4,
        "billYear" : 2020
    }
    {
        "_id" : 6,
        "name" : "Mobile",
        "totalPrice" : 25000,
        "totalQuantity" : 2,
        "billYear" : 2021
    }
    {
        "_id" : 7,
        "name" : "Memory Card",
        "totalPrice" : 1000,
        "totalQuantity" : 10,
        "billYear" : 2019
    }
    {
        "_id" : 8,
        "name" : "Pen drive",
        "totalPrice" : 15000,
        "totalQuantity" : "Two",
        "billYear" : 2018
    }
    {
        "_id" : 9,
        "name" : "Laptop",
        "billDetail" : {
                               "totalPrice" : 45000,
                               "totalQuantity" : 1,
                              }
        "billYear" : 2021
    }
    {
        "_id" : 10,
        "name" : "Memory Carde",
        "totalPrice" : 4000,
        "totalQuantity" : 50,
        "billYear" : 2020
    }
}

Example 1: Using $divide operator

In this example, we are going to use the $divide operator to find the price of a mobile.

Output:

{
        "_id" : 1,
        "name" : "Mobile",
        "totalPrice" : 100000,
        "totalQuantity" : 50,
        "mobilePrice" : 2000
}
{
        "_id" : 5,
        "name" : "Mobile",
        "totalPrice" : 20000,
        "totalQuantity" : 4,
        "mobilePrice" : 5000
}
{
        "_id" : 6,
        "name" : "Mobile",
        "totalPrice" : 25000,
        "totalQuantity" : 2,
        "mobilePrice" : 12500
}

Example 2: Add your own number

If the user wants to divide a field by a certain number, the user can do so. In this example, we're going to divide the "totalPrice" field by 2.

Output:

{
        "_id" : 4,
        "name" : "Memory Card",
        "totalPrice" : 2500,
        "halfPrice" : 1250
}
{
        "_id" : 7,
        "name" : "Memory Card",
        "totalPrice" : 1000,
        "halfPrice" : 500
}
{
        "_id" : 10,
        "name" : "Memory Carde",
        "totalPrice" : 4000,
        "halfPrice" : 2000
}

Example 3: Wrong data type

The $divide operator only supports integer data types. If the value is not an integer, it gives an error.

Output:

uncaught exception: Error: command failed: {
	"ok" : 0,
	"errmsg" : "$divide only supports numeric types, not string and double",
	"code" : 16611,
	"codeName" : "Location16611"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:639:17
assert.commandWorked@src/mongo/shell/assert.js:729:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12
@(shell):1:1

Example 4: Using $divide operator in the embedded document

In this example, we're going to divide the total price of the laptop by 2.

Output:

{
        "_id" : 9,
        "name" : "Laptop",
        "halfPrice" : 22500
}






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