Skip to content

Commit e02502b

Browse files
jason-price-mongodbjason-price-mongodb
authored andcommitted
DOCS-14662 mod match expression values (#6017) (#6018)
Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 32b04c8 commit e02502b

File tree

1 file changed

+33
-19
lines changed
  • source/reference/operator/query

1 file changed

+33
-19
lines changed

source/reference/operator/query/mod.txt

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,36 @@ $mod
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

2839
Examples
2940
--------
3041

3142
Use ``$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

4255
Then, 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
5063
The 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:
7589
The 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

8496
Empty Array
8597
```````````
@@ -94,11 +106,9 @@ an empty array:
94106
The 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

121135
Floating Point Arguments
122136
~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)