-
Type:
Bug
-
Resolution: Won't Fix
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 2.4.5, 2.5.4
-
Component/s: Write Ops
-
Query Execution
-
ALL
-
QE 2025-12-08
-
None
-
None
-
None
-
None
-
None
-
None
-
None
$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