Skip to main content

$mod

The $mod operator returns the remainder after dividing the first numeric value by the second numeric value.

Input

  • An array containing exactly two numeric values (dividend and divisor)

Output

  • The remainder after division (first % second)

Examples

Basic Modulo

{
  "$mod": [
    10,
    3
  ]
}
Result: 1

Field Value Modulo

{
  "$mod": [
    {
      "$input": "item_count"
    },
    {
      "$input": "group_size"
    }
  ]
}
Result: The remainder when item_count is divided by group_size

Decimal Modulo

{
  "$mod": [
    10.5,
    3.2
  ]
}
Result: 4.1

Modulo by Zero Error

{
  "$mod": [
    10,
    0
  ]
}
Result: Error (division by zero)