2121
2222 { field: { $mod: [ divisor, remainder ] } }
2323
24- The :query:`$mod` operator errors when passed an array with fewer
25- or more than two elements. See :ref:`mod-not-enough-elements` and
26- :ref:`mod-too-many-elements` for details.
24+ Behavior
25+ --------
26+
27+ The :query:`$mod` operator returns an error if the:
28+
29+ - ``[ divisor, remainder ]`` array contains fewer or more than
30+ two elements. For examples, see :ref:`mod-not-enough-elements` and
31+ :ref:`mod-too-many-elements` respectively.
32+
33+ - ``divisor`` or ``remainder`` values evaluate to:
34+
35+ - ``NaN`` (not a number) or ``Infinity``.
36+
37+ - A value that cannot be represented using a 64-bit integer.
2738
2839Examples
2940--------
3041
3142Use ``$mod`` to Select Documents
3243~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3344
34- Consider a collection ``inventory`` with the following documents :
45+ Create an ``inventory`` collection :
3546
3647.. code-block:: javascript
3748
38- { "_id" : 1, "item" : "abc123", "qty" : 0 }
39- { "_id" : 2, "item" : "xyz123", "qty" : 5 }
40- { "_id" : 3, "item" : "ijk123", "qty" : 12 }
49+ db.inventory.insertMany( [
50+ { "_id" : 1, "item" : "abc123", "qty" : 0 },
51+ { "_id" : 2, "item" : "xyz123", "qty" : 5 },
52+ { "_id" : 3, "item" : "ijk123", "qty" : 12 }
53+ ] )
4154
4255Then, the following query selects those documents in the
4356``inventory`` collection where value of the ``qty`` field modulo
@@ -50,6 +63,7 @@ Then, the following query selects those documents in the
5063The query returns the following documents:
5164
5265.. code-block:: javascript
66+ :copyable: false
5367
5468 { "_id" : 1, "item" : "abc123", "qty" : 0 }
5569 { "_id" : 3, "item" : "ijk123", "qty" : 12 }
@@ -75,11 +89,9 @@ an array that contains a single element:
7589The statement results in the following error:
7690
7791.. code-block:: javascript
92+ :copyable: false
7893
79- error: {
80- "$err" : "bad query: BadValue malformed mod, not enough elements",
81- "code" : 16810
82- }
94+ MongoServerError: malformed mod, not enough elements
8395
8496Empty Array
8597```````````
@@ -94,11 +106,9 @@ an empty array:
94106The statement results in the following error:
95107
96108.. code-block:: javascript
109+ :copyable: false
97110
98- error: {
99- "$err" : "bad query: BadValue malformed mod, not enough elements",
100- "code" : 16810
101- }
111+ MongoServerError: malformed mod, not enough elements
102112
103113.. _mod-too-many-elements:
104114
@@ -113,10 +123,14 @@ operator with an array that contains four elements:
113123
114124.. code-block:: javascript
115125
116- error: {
117- "$err" : "bad query: BadValue malformed mod, too many elements",
118- "code" : 16810
119- }
126+ db.inventory.find( { qty: { $mod: [ 4, 1, 2, 3 ] } } )
127+
128+ The statement results in the following error:
129+
130+ .. code-block:: javascript
131+ :copyable: false
132+
133+ MongoServerError: malformed mod, too many elements
120134
121135Floating Point Arguments
122136~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments