MongoDB $mod 运算符
MongoDB中提供了不同类型的在聚合流水线阶段使用和$ mod运算符就是其中之一rithmetic表达式运算符。此运算符用于将一个数除以另一个数并返回余数。
句法:
{ $mod: [ , ] }
这里,在这个运算符,参数以数组形式传递。它需要两个参数,第一个参数是被除数,第二个参数是除数。吨他参数必须是一个有效的表达式,直到它解析为一个数字。
例子:
在以下示例中,我们正在使用:
Database: GeeksforGeeks
Collection: example
Document: two documents that contain the details of the shapes in the form of field-value pairs.
使用 $mod运算符查找提醒:
在这个例子中,我们将使用 $mod 运算符将边字段的值(即 4)除以 2 来找到余数。
db.example.aggregate([{$match:{name: "Square"}},
... {$project: {remainderValue: {$mod: ["$side", 2]}}}])
使用 $mod运算符在嵌入文档中查找提醒:
在此示例中,我们将使用 $mod运算符将measurement.width 字段的值(即15)除以measurement.height 字段的值(即10)来求余数。
db.example.aggregate([{$match:{name: "Rectangle"}},
... {$project: {remainderValue: {$mod: ["$measurement.width",
"$measurement.height"]}}}])