[SERVER-14391] Upsert with $all query and $addToSet operation errors Created: 30/Jun/14  Updated: 10/Dec/14  Resolved: 30/Jun/14

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.6.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Michael Yoffe Assignee: Unassigned
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-3946 Update with $addToSet/$push with a qu... Closed
Operating System: ALL
Participants:

 Description   

With an empty collection, run this:

db.Food.update(
    { "names" : { "$all" : [ "corn"]}},
    { "$addToSet" : { "names" : "corn"}},
    { upsert: true });

Results in this error:
Cannot apply $addToSet to a non-array field. Field named 'names' has a non-array type String in the document INVALID-MUTABLE-ELEMENT

Tested in 2.6.3
It worked fine in 2.4.10



 Comments   
Comment by Scott Hernandez (Inactive) [ 30/Jun/14 ]

As you have found it errors without the $all, which is really just a long version for equality, on 2.4 and 2.6 as it is designed to.

// 2.4
> db.Food.update( { "names" : "corn"}, { "$addToSet" : { "names" : "corn"}},  { upsert: true });
WriteResult({
...
		"errmsg" : "Cannot apply $addToSet modifier to non-array"}})
 
// 2.6
> db.Food.update( { "names" : "corn"}, {"$addToSet" : {"names" : "corn"}}, {upsert: true });
WriteResult({
...
		"errmsg" : "Cannot apply $addToSet to a non-array field. Field named 'names' has a non-array type String in the document INVALID-MUTABLE-ELEMENT" }})

The behavior you are seeing with 2.4 + $all is just a bug that has been remedied in 2.6 when we re-wrote both the query + update frameworks.

Comment by Michael Yoffe [ 30/Jun/14 ]

I found that it's unrelated to $all.
This example results in the same error:

db.Food.update(
    { "names" : "corn"},
    { "$addToSet" : { "names" : "corn"}},
    { upsert: true });

Generated at Thu Feb 08 03:34:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.