There is an issue (possibly a bug) and a question (possibly incompatible change).
=======
The issue. Steps to reproduce:
Step 1.
Add an object
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
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?