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 ] } }
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), 此外,从MongoDB 5.1(以及5.0.4和4.4.10)开始,如果$mod
returns an error if the divisor
or remainder
values evaluate to:divisor
或remainder
值的计算结果为:
NaN
(not a number) or Infinity
.NaN
(不是数字)或无限Infinity
。$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 }
The 当传递少于两个元素的数组时,$mod
operator errors when passed an array with fewer than two elements.$mod
运算符出错。
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
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
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
The $mod
expression rounds decimal input towards zero.$mod
表达式将十进制输入四舍五入为零。
The following examples demonstrate this behavior:以下示例演示了此行为:
Input query:输入查询:
db.inventory.find( { qty: { $mod: [ 4.0, 0 ] } } )
Results:结果:
{ _id: 1, item: 'abc123', qty: 0 } { _id: 3, item: 'ijk123', qty: 12 }
Input query:输入查询:
db.inventory.find( { qty: { $mod: [ 4.5, 0 ] } } )
Results:结果:
{ _id: 1, item: 'abc123', qty: 0 } { _id: 3, item: 'ijk123', qty: 12 }
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
表达式,而不管小数点是多少,结果都是相同的结果集。