Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions source/reference/operator/query/mod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,36 @@ $mod

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

The :query:`$mod` operator errors when passed an array with fewer
or more than two elements. See :ref:`mod-not-enough-elements` and
:ref:`mod-too-many-elements` for details.
Behavior
--------

The :query:`$mod` operator returns an error if the:

- ``[ divisor, remainder ]`` array contains fewer or more than
two elements. For examples, see :ref:`mod-not-enough-elements` and
:ref:`mod-too-many-elements` respectively.

- ``divisor`` or ``remainder`` values evaluate to:

- ``NaN`` (not a number) or ``Infinity``.

- A value that cannot be represented using a 64-bit integer.

Examples
--------

Use ``$mod`` to Select Documents
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Consider a collection ``inventory`` with the following documents:
Create an ``inventory`` collection:

.. code-block:: javascript

{ "_id" : 1, "item" : "abc123", "qty" : 0 }
{ "_id" : 2, "item" : "xyz123", "qty" : 5 }
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }
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
Expand All @@ -50,6 +63,7 @@ Then, the following query selects those documents in the
The query returns the following documents:

.. code-block:: javascript
:copyable: false

{ "_id" : 1, "item" : "abc123", "qty" : 0 }
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }
Expand All @@ -75,11 +89,9 @@ an array that contains a single element:
The statement results in the following error:

.. code-block:: javascript
:copyable: false

error: {
"$err" : "bad query: BadValue malformed mod, not enough elements",
"code" : 16810
}
MongoServerError: malformed mod, not enough elements

Empty Array
```````````
Expand All @@ -94,11 +106,9 @@ an empty array:
The statement results in the following error:

.. code-block:: javascript
:copyable: false

error: {
"$err" : "bad query: BadValue malformed mod, not enough elements",
"code" : 16810
}
MongoServerError: malformed mod, not enough elements

.. _mod-too-many-elements:

Expand All @@ -113,10 +123,14 @@ operator with an array that contains four elements:

.. code-block:: javascript

error: {
"$err" : "bad query: BadValue malformed mod, too many elements",
"code" : 16810
}
db.inventory.find( { qty: { $mod: [ 4, 1, 2, 3 ] } } )

The statement results in the following error:

.. code-block:: javascript
:copyable: false

MongoServerError: malformed mod, too many elements

Floating Point Arguments
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down