Docs HomeMongoDB Manual

$mod

On this page本页内容

$mod

Select documents where the value of a field divided by a divisor has the specified remainder (i.e. perform a modulo operation to select documents). 选择字段值除以除数后具有指定余数的文档(即执行模运算以选择文档)。To specify a $mod expression, use the following syntax:要指定$mod表达式,请使用以下语法:

{ field: { $mod: [ divisor, remainder ] } }

Behavior行为

The $mod operator returns an error if the [ divisor, remainder ] array contains fewer or more than two elements. 如果[ divisor, remainder ]数组包含少于或多于两个元素,$mod运算符将返回一个错误。For examples, see Not Enough Elements Error and Too Many Elements Error respectively.例如,请分别参见元素不足错误元素过多错误

Also, starting in MongoDB 5.1 (and 5.0.4 and 4.4.10), $mod returns an error if the divisor or remainder values evaluate to:此外,从MongoDB 5.1(以及5.0.4和4.4.10)开始,如果divisorremainder的值计算为以下情况,则$mod返回错误:

  • NaN (not a number).(不是数字)。
  • Infinity.
  • A value that can't be represented using a 64-bit integer.无法使用64位整数表示的值。

If a document in the collection contains a field where the value is NaN (not a number) or Infinity, $mod doesn't include the document in the output.如果集合中的文档包含值为NaN(不是数字)或Infinity的字段,$mod不会在输出中包含该文档。

Examples实例

Use $mod to Select Documents使用$mod选择文档

Create an inventory collection:创建inventory集合:

db.inventory.insertMany( [
{ "_id" : 1, "item" : "abc123", "qty" : 0 },
{ "_id" : 2, "item" : "xyz123", "qty" : 5 },
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }
] )

Then, the following query selects those documents in the inventory collection where value of the qty field modulo 4 equals 0:然后,以下查询选择inventory集合中qty字段模4值等于0的单据:

db.inventory.find( { qty: { $mod: [ 4, 0 ] } } )

The query returns the following documents:查询返回以下文档:

{ "_id" : 1, "item" : "abc123", "qty" : 0 }
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }

Not Enough Elements Error元素不足错误

The $mod operator errors when passed an array with fewer than two elements.$mod运算符在传递少于两个元素的数组时出错。

Array with Single Element具有单个元素的数组

The following operation incorrectly passes the $mod operator an array that contains a single element:以下操作错误地将包含单个元素的数组传递给$mod运算符:

db.inventory.find( { qty: { $mod: [ 4 ] } } )

The statement results in the following error:该语句导致以下错误:

MongoServerError: malformed mod, not enough elements

Empty Array空数组

The following operation incorrectly passes the $mod operator an empty array:以下操作错误地将空数组传递给$mod运算符:

db.inventory.find( { qty: { $mod: [ ] } } )

The statement results in the following error:该语句导致以下错误:

MongoServerError: malformed mod, not enough elements

Too Many Elements Error元素太多错误

The $mod operator errors when passed an array with more than two elements.$mod运算符在传递包含两个以上元素的数组时出错。

For example, the following operation attempts to use the $mod operator with an array that contains four elements:例如,以下操作尝试将$mod运算符与包含四个元素的数组一起使用:

db.inventory.find( { qty: { $mod: [ 4, 1, 2, 3 ] } } )

The statement results in the following error:该语句导致以下错误:

MongoServerError: malformed mod, too many elements

Floating Point Arguments浮点参数

The $mod expression rounds decimal input towards zero.$mod表达式将十进制输入四舍五入为零。

The following examples demonstrate this behavior:以下示例演示了这种行为:

Example

Input query:输入查询:

db.inventory.find( { qty: { $mod: [ 4.0, 0 ] } } )

Results:结果:

{ _id: 1, item: 'abc123', qty: 0 }
{ _id: 3, item: 'ijk123', qty: 12 }
Example

Input query:输入查询:

db.inventory.find( { qty: { $mod: [ 4.5, 0 ] } } )

Results:结果:

{ _id: 1, item: 'abc123', qty: 0 }
{ _id: 3, item: 'ijk123', qty: 12 }
Example

Input query:输入查询:

db.inventory.find( { qty: { $mod: [ 4.99, 0 ] } } )

Results:结果:

{ _id: 1, item: 'abc123', qty: 0 }
{ _id: 3, item: 'ijk123', qty: 12 }

Each query applies 4 to the $mod expression regardless of decimal points, resulting in the same result set.每个查询将4应用于$mod表达式,而不考虑小数点,从而产生相同的结果集。