- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Arithmetic Aggregation Operators >
- $mod (aggregation)
$mod (aggregation)¶
On this page
Definition¶
- $mod¶
Divides one number by another and returns the remainder.
The $mod expression has the following syntax:
{ $mod: [ <expression1>, <expression2> ] }
The first argument is the dividend, and the second argument is the divisor; i.e. first argument is divided by the second argument.
The arguments can be any valid expression as long as they resolve to numbers. For more information on expressions, see Expressions.
Example¶
Consider a planning collection with the following documents:
{ "_id" : 1, "project" : "A", "hours" : 80, "tasks" : 7 }
{ "_id" : 2, "project" : "B", "hours" : 40, "tasks" : 4 }
The following aggregation uses the $mod expression to return the remainder of the hours field divided by the tasks field:
db.planning.aggregate(
[
{ $project: { remainder: { $mod: [ "$hours", "$tasks" ] } } }
]
)
The operation returns the following results:
{ "_id" : 1, "remainder" : 3 }
{ "_id" : 2, "remainder" : 0 }
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.