$mod (aggregation)
On this page本页内容
Definition定义
$mod
-
Divides one number by another and returns the remainder.将一个数字除以另一个数字并返回余数。The$mod
expression has the following syntax:$mod
表达式具有以下语法:{ $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 考虑一个包含以下文档的conferencePlanning
collection with the following documents:conferencePlanning
(会议计划)集合:
db.conferencePlanning.insertMany( [
{ "_id" : 1, "city" : "New York", "hours" : 80, "tasks" : 7 },
{ "_id" : 2, "city" : "Singapore", "hours" : 40, "tasks" : 4 }
] )
The following aggregation uses the 以下聚合使用$mod
expression to return the remainder of the hours
field divided by the tasks
field:$mod
表达式返回hours
字段除以tasks
字段的余数部分:
db.conferencePlanning.aggregate(
[
{ $project: { remainder: { $mod: [ "$hours", "$tasks" ] } } }
]
)
The operation returns the following results:该操作返回以下结果:
{ "_id" : 1, "remainder" : 3 }
{ "_id" : 2, "remainder" : 0 }