[SERVER-3946] Update with $addToSet/$push with a query on same field is broken during insert Created: 26/Sep/11 Updated: 06/Dec/22 Resolved: 21/Apr/17 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Write Ops |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Eliot Horowitz (Inactive) | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Done | Votes: | 5 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||
| Assigned Teams: |
Query
|
||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||
| Description |
Cannot apply $addToSet modifier to non-array |
| Comments |
| Comment by David Storch [ 21/Apr/17 ] | ||||||||||
|
Hi all, After some discussion on related ticket
this means that we will first create the document {x: "a"}, since there was a single equality predicate on x. Then, we will attempt to apply {$addToSet: {x: "b"}} to this document. This is an error, since $addToSet can only be done against array fields. In order to avoid this problem, you should wrap your equality predicate in an $elemMatch:
Since queries like this always expect the field x to be an array, I imagine that the $elemMatch version of the problematic query is sufficient for nearly all use cases. You may get into trouble if your schema has a field which can be either an array or a scalar, but such mixed array/scalar fields are not advisable. In a mixed array/scalar schema, an $addToSet query such as the one reported here will already fail if the upsert operation happens to match a document containing a scalar. Please let me know if you have any questions or concerns. Best, | ||||||||||
| Comment by David Storch [ 24/Feb/17 ] | ||||||||||
|
Hi zoellner@qollaboration.com, yes, single-element $in behavior has changed for upsert in 3.4. This is being tracked by | ||||||||||
| Comment by Andreas Zoellner [ 24/Feb/17 ] | ||||||||||
|
The above "working" version doesn't work for us. Might be due to updates in 3.2/3.4. The best workaround for us right now seems to be to use $elemMatch in the query: This works on 3.4.1:
| ||||||||||
| Comment by Constantin Guay [ 26/Dec/14 ] | ||||||||||
|
Tested in version 2.6.6:
I had to use:
To make it works. | ||||||||||
| Comment by Suriel Bendahan [ 25/Nov/13 ] | ||||||||||
|
As a workaround you can use the $all modifier in the query supplying it with a single valued array matching your lookup:
| ||||||||||
| Comment by Simon Schenk [ 27/Sep/12 ] | ||||||||||
|
Are there any plans, when this issue will be fixed? Its been a real pain for me, as working around it requires a query and then a separate update/insert and that combination affects performance really badly. |