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

$unset should get an error on negative array indices, like $pop and $pull do

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.6.0-rc2
    • Component/s: Write Ops
    • None

      $unset should get an error on negative array indices, like $pop and $pull do

      Step 1.
      Add a document with an array:

      > db.test.drop()
      > db.test.insert({_id : 1, a : [1, 2]})
      

      Step 2.
      Try to $unset at an invalid negative index:

      > db.test.update({_id : 1}, {$unset : {"a.-2" : 1}})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
      

      Actual result:
      The operation is silently ignored.

      Expected result:
      An error should be returned, like in $pop and $pull cases.

      Step 3.
      Compare with $pop and $pull invoked with negative indices, they return expected errors:

      > db.test.update({_id : 1}, {$pop : {"a.-2" : 1}})
      WriteResult({
              "nMatched" : 0,
              "nUpserted" : 0,
              "nModified" : 0,
              "writeError" : {
                      "code" : 16837,
                      "errmsg" : "cannot use the part (a of a.-2) to traverse the element ({a: [ 1.0, 2.0 ]})"
              }
      })
      
      > db.test.update({_id : 1}, {$pull : {"a.-2" : 1}})
      WriteResult({
              "nMatched" : 0,
              "nUpserted" : 0,
              "nModified" : 0,
              "writeError" : {
                      "code" : 16837,
                      "errmsg" : "cannot use the part (a of a.-2) to traverse the element ({a: [ 1.0, 2.0 ]})"
              }
      })
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            nightroman Roman Kuzmin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: