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

v2.6.0-rc0 update $addToSet does not add to "a.1" and similar items

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.6.0-rc1
    • Affects Version/s: 2.6.0-rc0
    • Component/s: Write Ops
    • None
    • ALL

      There is an issue (possibly a bug) and a question (possibly incompatible change).

      =======

      The issue. Steps to reproduce:

      Step 1.
      Add an object

      { "_id" : 1, "a" : [ 1, [ ] ] }

      to the empty collection test:

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

      Step 2.
      Let's try to add 1 to the empty array 'a[1]' which is the second item of the array 'a':

      > db.test.update({ "_id" : 1 }, { $addToSet : { "a.1" : 1 } })
      WriteResult({
              "nMatched" : 0,
              "nUpserted" : 0,
              "nModified" : 0,
              "writeError" : {
                      "code" : 57,
                      "errmsg" : "a.1 is not valid for storage."
              }
      })
      

      The command fails telling "a.1 is not valid for storage.". At the same time a.1 seems to a valid item for $addToSet, i.e. it is an array. This is either a bug or a incompatible change because this used to work fine in v2.4.9.

      =======

      The question. Steps to reproduce:

      Step 1.
      Add an object

      { "_id" : 1, "a" : [ 1, 2 ] }

      to the empty collection test:

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

      Step 2.
      Let's try to add 1 to non existent item 'a[3]' of the array 'a':

      > db.test.update({ "_id" : 1 }, { $addToSet : { "a.3" : 1 } })
      WriteResult({
              "nMatched" : 0,
              "nUpserted" : 0,
              "nModified" : 0,
              "writeError" : {
                      "code" : 57,
                      "errmsg" : "a.3 is not valid for storage."
              }
      })
      

      It fails "a.3 is not valid for storage.". Although it may look reasonable, it used to work fine in v2.4.9. The result was

      { "_id" : 1, "a" : [ 1, 2, null, [1] ] }

      , i.e. 'a' was appended with nulls and an array [1] was inserted at the index 3.

      The question: is this new behaviour intended?

            Assignee:
            andrew.morrow@mongodb.com Andrew Morrow (Inactive)
            Reporter:
            nightroman Roman Kuzmin
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: