Details
-
Bug
-
Resolution: Won't Fix
-
Major - P3
-
mongodb-2.6, mongodb-3.0
-
None
-
0.25
Description
The $mod conditional operator casts all its inputs into LONG's before executing the modulo calculation and comparison. This truncates any decimal values back to their integer parts, which causing some users to get unexpected results. For example, if db.collName.f1 holds [0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, ... 9.0, 9.5, 10] then these statements:
db.collName.find({ "f1": {$mod:[5.5, 2.5]} })
|
db.collName.find({ "f1": {$mod:[5, 2]} })
|
both return [2.0, 2.5, 7.0, 7.5] because we don't return the documents where ((f1 % 5.5) == 2.5), we return those where ((floor(f1) % floor(5.5)) == floor(2.5)).
While I accept that MongoDB does this "integer division" because it stores numbers in binary floating point and floating point calculations are prone to rounding errors, I think that we should make this clearer in the manual entry for $mod (https://docs.mongodb.org/manual/reference/operator/query/mod/). Most users expect that mod will behave the same way as it does in their other databases/applications/programming environments/etc because it is a generic mathematical term.
Attachments
Issue Links
- is related to
-
SERVER-1393 Support decimal numbers
-
- Closed
-
- related to
-
SERVER-32960 $mod has inconsistent rounding/truncation behavior
-
- Closed
-
-
SERVER-32961 Add $fmod, or some way to mod by fractional numbers, to the match expression language
-
- Backlog
-