Navigation
This version of the documentation is archived and no longer supported.

$mod

$mod

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

$mod selects the documents where the field value divided by the divisor has the specified remainder.

Consider the following example:

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

This query will select all documents in the inventory collection where the qty field value modulo 4 equals 0, such as documents with qty value equal to 0 or 12.

In some cases, you can query using the $mod operator rather than the more expensive $where operator. Consider the following example using the $mod operator:

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

The above query is less expensive than the following query which uses the $where operator:

db.inventory.find( { $where: "this.qty % 4 == 0" } )

See also

find(), update(), $set.