[SERVER-23664] $mod match expression should raise an error when the remainder is not a number Created: 12/Apr/16  Updated: 04/Jan/22  Resolved: 08/Oct/19

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 4.3.1, 4.2.19

Type: Bug Priority: Major - P3
Reporter: Graeme Yeates Assignee: James Wahlin
Resolution: Done Votes: 0
Labels: neweng, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Duplicate
is duplicated by SERVER-61430 Raise an error when $mod remainder is... Closed
Related
Backwards Compatibility: Minor Change
Operating System: ALL
Backport Requested:
v4.2
Sprint: Query 2019-10-21
Participants:

 Description   

Other query operators treat null as NaN such as $lt, $gt, $lte, $gte

$mod treats null as 0 rather than NaN and will return documents

Semi related: SERVER-11414



 Comments   
Comment by Githook User [ 17/Dec/21 ]

Author:

{'name': 'Romans Kasperovics', 'email': 'romans.kasperovics@mongodb.com', 'username': 'romanskas'}

Message: SERVER-23664 $mod match expression should raise an error when the remainder is not a number
Branch: v4.2
https://github.com/mongodb/mongo/commit/aa3a5ed81449d7f8bef523b4372352bd72c8d93e

Comment by Githook User [ 08/Oct/19 ]

Author:

{'username': 'jameswahlin', 'email': 'james.wahlin@mongodb.com', 'name': 'James Wahlin'}

Message: SERVER-23664 $mod match expression should raise an error when the remainder is not a number
Branch: master
https://github.com/mongodb/mongo/commit/50fd4549162cd71617cabbb4e802a44c51f8afc2

Comment by David Storch [ 26/Jul/19 ]

This appears to be caused by a simple typo on this line: https://github.com/mongodb/mongo/blob/dc4db514a1ee737db0553f9535033453502b3ac7/src/mongo/db/matcher/expression_parser.cpp#L450

The following one-letter change causes the server to correctly raise an error when the remainder is not numeric:

diff --git a/src/mongo/db/matcher/expression_parser.cpp b/src/mongo/db/matcher/expression_parser.cpp
index 811c0f323e..036a5c9db5 100644
--- a/src/mongo/db/matcher/expression_parser.cpp
+++ b/src/mongo/db/matcher/expression_parser.cpp
@@ -447,7 +447,7 @@ StatusWithMatchExpression parseMOD(StringData name, BSONElement e) {
     if (!i.more())
         return {Status(ErrorCodes::BadValue, "malformed mod, not enough elements")};
     auto r = i.next();
-    if (!d.isNumber())
+    if (!r.isNumber())
         return {Status(ErrorCodes::BadValue, "malformed mod, remainder not a number")};
 
     if (i.more())

I'm flagging this for re-triage.

Comment by Ian Whalen (Inactive) [ 15/Apr/16 ]

this should be an error

Comment by Kelsey Schubert [ 13/Apr/16 ]

Hi yeatesgraeme@gmail.com,

Thank you for opening this ticket. I'd like to summarize the issue that you are describing.

Currently, the behavior of $mod with a null remainder can be seen below:

db.foo.find({x : {$mod : [5,null]}})
{ "_id" : ObjectId("570d586febbec3e7b4a0f30b"), "x" : 5 }

where null is treated as 0, and documents may be returned. The expectation is that null would be treated as NaN and no documents would be returned.

I am marking this ticket to be scheduled during the next round of planning. Please continue to watch for updates.

Kind regards,
Thomas

Generated at Thu Feb 08 04:04:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.