Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-4561

$inc should throw an error when trying to increment by NaN

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.4.5, 2.5.4
    • Component/s: Write Ops
    • Query Execution
    • ALL

      $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 
      

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            elubow Eric Lubow
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: