[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:
Related
related to DOCS-1114 Sets are different than arrays, or wi... Closed
is related to SERVER-8526 Validate modifiers for updates, error... Closed
Participants:

 Description   

SERVER-991 is about adding $slice to $push, but scotthernandez says it should also be supported for $addToSet (or throw an error).



 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:

  • I have a collection in which documents have one field which is an array of integers;
  • I want to add values to those arrays as fast as possible, without fetching any document, and in one atomic operation;
  • I don't want to push the integer in the array if it's already in, and I want the array to have a maximum size.

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.
I think long term we'll want it to actually be a set, not an array, so dont' want any more array ops on it.

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.

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