MongoDB $mod Operator

What is the $mod operator?

MongoDB provides a variety of arithmetic expression operators. The $mod operator is one of those operators. The $mod operator is used in the aggregation-pipeline stages. The $mod operator is used to divide one number by another number and return the remainder. In other words, the first number is the dividend, and the second number is the divisor.

Syntax of the $log operator:

Thecan be any valid expression that resolves to numbers.

Examples:

In the following examples, we are working with:

>db.items.find().pretty()
{
    {
        "_id" : 1,
        "item_name" : "Apple",
        "total_Price" : null,
        "quantity" : 40,
    }
    {
        "_id" : 2,
        "item_name" : "Banana",
        "total_Price" : 1000,
        "quantity" : 72,
    }
    {
        "_id" : 3,
        "item_name" : "Cherry",
        "total_Price" : 215,
        "quantity" : 25,
    }
    {
        "_id" : 4,
        "item_name" : "Apple",
        "total_Price" : null,
        "quantity" : 25,
    }
    {
        "_id" : 5,
        "item_name" : "Banana",
        "total_Price" : 400,
        "quantity" : 35,
    }
    {
        "_id" : 6,
        "item_name" : "Banana",
        "total_Price" : 510,
        "quantity" : 100,
    }
    {
        "_id" : 7,
        "item_name" : "Cherry",
        "total_Price" : 500,
        "quantity" : 41,
    }
    {
        "_id" : 8,
        "item_name" : "Rasbhari",
        "total_Price" : 80,
        "quantity" : "Ten",
    }
    {
        "_id" : 9,
        "item_name" : "Banana",
        "total_Price" : 205,
        "quantity" : 10,
    }
    {
        "_id" : 10,
        "item_name" : "Apple",
        "total_Price" : 95,
        "quantity" : null,
    }
}

Example 1: Finding reminder using $mod operator

In this example, we're going to find the remainder by dividing the value of the "total_Price" field by the "quantity" field using the $mod operator.

Output:

{
    "_id" : 2,
    "item_name" : "Banana",
    "total_Price" : 1000,
    "quantity" : 72,
    "remainderValue" : 64
}
{
    "_id" : 5,
    "item_name" : "Banana",
    "total_Price" : 400,
    "quantity" : 35,
    "remainderValue" : 15
}
{
    "_id" : 6,
    "item_name" : "Banana",
    "total_Price" : 510,
    "quantity" : 100,
    "remainderValue" : 10
}
{
    "_id" : 9,
    "item_name" : "Banana",
    "total_Price" : 205,
    "quantity" : 10,
    "remainderValue" : 5
}  

Example 2: Wrong data type

The $mod operator supports only numeric data types. If the value is a string, the $mod operator returns the error.

Output:

uncaught exception: Error: command failed: {
	"ok" : 0,
	"errmsg" : "$mod 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 3: Null values

In this example, we're going to find the remainder by dividing the value of the "total_Price" field by the "quantity" field in the apple using the $mod operator.

Output:

{
    "_id" : 1,
    "item_name" : "Apple",
    "total_Price" : null,
    "quantity" : 40,
    "remainderValue" : null
}
{
    "_id" : 4,
    "item_name" : "Apple",
    "total_Price" : null,
    "quantity" : 25,
    "remainderValue" : null    
}
{
    "_id" : 10,
    "item_name" : "Apple",
    "total_Price" : 95,
    "quantity" : null,
    "remainderValue" : null
}   





Latest Courses