$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 值的计算结果为:

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

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表达式,而不管小数点是多少,结果都是相同的结果集。

←  $jsonSchema$regex →