[SERVER-8512] support $slice/sort in $addToSet Created: 11/Feb/13 Updated: 06/Nov/15 Resolved: 12/Feb/13 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | 2.4.0-rc0 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Derick Rethans | Assignee: | Unassigned |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | operations | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Participants: | |||||||||||||
| Description |
|
|
| Comments |
| Comment by Tom Brückner [ 06/Nov/15 ] |
|
I ran in the same problem, and the reason for closing this request is quite unlogical to me. My use case: I want to store a list of subjects a user is familar with. This should be a unique, sorted list of strings. If $addToSet is only meant for sets, how else can one prevent to add an already existing array element twice and maintain sort order at the same time? It does not make any sense to me to do this in two subsequent updates. |
| Comment by Paulo Diniz [ 06/Nov/15 ] |
|
I agree with Guillaume Gelin. Specially once you add support for $each to $addToSet it seems like a simple step to be able to add a sort. Im trying to maintain a sorted ordered list, and currently the only way to do that is to first add the elements with $addToSet and then sort them with an empty $push. If $addToSet is meant to be used only for sets it shouldnt be under the array section and have this "The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array." as it's description. |
| Comment by Guillaume Gelin [ 04/Nov/14 ] |
|
Sorry to put this back up, but those differences between $push and $addToSet look really inconsistent to me. My scenario is the following:
It seems to me that a fairly logical way of doing this would be {$addToSet: {"v": {$each: [value], $slice: size}}}. Actually, I don't see any way to do this at the moment. Edit: obviously, that would suppose that $addToSet could handle inclusion order, and I understand the problem in terms. Maybe it could be done in a new array modifier, or as a new $push modifier. |
| Comment by Scott Hernandez (Inactive) [ 12/Feb/13 ] |
|
Okay, we need to change the docs to make that clear and error out if there are invalide $fields in the modifiers/selectors. |
| Comment by Eliot Horowitz (Inactive) [ 12/Feb/13 ] |
|
I don't want to guarantee that $addToSet puts new items on the end, or can maintain order in anyway. |
| Comment by Scott Hernandez (Inactive) [ 12/Feb/13 ] |
|
All array modifiers should work the same. If you can $push with some selectors then $addToSet should work the same. It may very be that the user wants the set sorted, and order is maintained in arrays, so using the new $slice/sort selectors make sense here as well. |
| Comment by Eliot Horowitz (Inactive) [ 12/Feb/13 ] |
|
sets are unordered, so don't think this makes sense. |