21
21
22
22
{ field: { $mod: [ divisor, remainder ] } }
23
23
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.
27
38
28
39
Examples
29
40
--------
30
41
31
42
Use ``$mod`` to Select Documents
32
43
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
44
34
- Consider a collection ``inventory`` with the following documents :
45
+ Create an ``inventory`` collection :
35
46
36
47
.. code-block:: javascript
37
48
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
+ ] )
41
54
42
55
Then, the following query selects those documents in the
43
56
``inventory`` collection where value of the ``qty`` field modulo
@@ -50,6 +63,7 @@ Then, the following query selects those documents in the
50
63
The query returns the following documents:
51
64
52
65
.. code-block:: javascript
66
+ :copyable: false
53
67
54
68
{ "_id" : 1, "item" : "abc123", "qty" : 0 }
55
69
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }
@@ -75,11 +89,9 @@ an array that contains a single element:
75
89
The statement results in the following error:
76
90
77
91
.. code-block:: javascript
92
+ :copyable: false
78
93
79
- error: {
80
- "$err" : "bad query: BadValue malformed mod, not enough elements",
81
- "code" : 16810
82
- }
94
+ MongoServerError: malformed mod, not enough elements
83
95
84
96
Empty Array
85
97
```````````
@@ -94,11 +106,9 @@ an empty array:
94
106
The statement results in the following error:
95
107
96
108
.. code-block:: javascript
109
+ :copyable: false
97
110
98
- error: {
99
- "$err" : "bad query: BadValue malformed mod, not enough elements",
100
- "code" : 16810
101
- }
111
+ MongoServerError: malformed mod, not enough elements
102
112
103
113
.. _mod-too-many-elements:
104
114
@@ -113,10 +123,14 @@ operator with an array that contains four elements:
113
123
114
124
.. code-block:: javascript
115
125
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
120
134
121
135
Floating Point Arguments
122
136
~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments