[SERVER-4561] $inc should throw an error when trying to increment by NaN Created: 26/Dec/11  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.4.5, 2.5.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Eric Lubow Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 0
Labels: $inc, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Execution
Operating System: ALL
Participants:

 Description   

$inc a field by NaN should return an error.

Side note should be whether $inc by Infinity should $set the value to Infinity.

Discussion summary from email below:

$inc a value by NaN sets the value to NaN. My question is, if the error message says that the modifier $inc allows for numbers only (see below),
and NaN (Not a Number) is clearly by definition not a number, then shouldn't the expected behavior either be setting the value to '0' or (preferably) returning an error. Similar logic goes for Infinity.

> db.people.find({}); 
{ "_id" : ObjectId("4d4212a98ef10d1e196d06ac"), "name" : "eliot", "num" : 15 } 
> db.people.update({"_id" : ObjectId("4d4212a98ef10d1e196d06ac")}, {$inc:{num:'a'}}); 
Modifier $inc allowed for numbers only 
> db.people.update({"_id" : ObjectId("4d4212a98ef10d1e196d06ac")}, {$inc:{num:NaN}}); 
> db.people.find({}); 
{ "_id" : ObjectId("4d4212a98ef10d1e196d06ac"), "name" : "eliot", "num" : NaN } 
> NaN instanceof Number 
false 
> db.people.update({"_id" : ObjectId("4d4212a98ef10d1e196d06ac")}, {$set:{num:5}}); 
> db.people.find({}); 
{ "_id" : ObjectId("4d4212a98ef10d1e196d06ac"), "name" : "eliot", "num" : 5 } 
> db.people.update({"_id" : ObjectId("4d4212a98ef10d1e196d06ac")}, {$inc:{num:Infinity}}); 
> db.people.find({}); 
{ "_id" : ObjectId("4d4212a98ef10d1e196d06ac"), "name" : "eliot", "num" : Infinity } 
> Infinity instanceof Number 
false 



 Comments   
Comment by Asya Kamsky [ 09/May/18 ]

Related observation: increment by NaN of NaN is recorded as an update with side effect and produces oplog entries and logs that show nModified:1.

(3.6.4) > db.inc.update({_id:1},{$set:{a:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
(3.6.4) > db.inc.update({_id:1},{$inc:{a:NaN}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
(3.6.4) > db.inc.update({_id:1},{$set:{a:NaN}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
(3.6.4) > db.inc.update({_id:1},{$inc:{a:NaN}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
(3.6.4) > db.inc.update({_id:1},{$inc:{a:NaN}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 

Generated at Thu Feb 08 03:06:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.