[SERVER-12537] Support $slice/sort in $addToSet Created: 29/Jan/14  Updated: 06/Dec/22  Resolved: 29/Jun/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.4.0-rc0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Glenn Maynard Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 3
Labels: operations
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Assigned Teams:
Query
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 Asya Kamsky [ 29/Jun/19 ]

SERVER-40381 implemented support for aggregation expressions to specify update for 4.2. 

You can see some examples here.

What remains to completely support use case in this ticket would be SERVER-29425

Comment by Ryan Shumate [ 23/May/17 ]

This would be useful in managing time series data in my schema design. We're handling millisecond resolution data that is stored in an array {'ts': ISODate("2017-05-23T10:50:00.0"), 'data': [

{'ms':1520, 'value':5.1351]}

. We'd prefer to use $addToSet over $push so that we can ignore duplicate entries for the same timestamp. However, using $addToSet doesn't allow us to sort the data on entry like $push. This requires an application side sort which is time consuming. Performing the sort on insert the same as $push would be very beneficial.

Comment by Glenn Maynard [ 29/Jan/14 ]

I've hit it a couple times. A simple example is storing a limited number of recently-seen IDs, for things like detecting accidental double-submission of an action. That's not exactly an MRU, since there's no need to move IDs which were already in the list to the end. (Of course, you can do simple cases with $push and a query, but that's always true for $addToSet and stops working when combined with other changes.)

I don't currently need sorting, that was just copied in by the clone.

Comment by Glenn Maynard [ 29/Jan/14 ]

(This is a clone of SERVER-8512. I'd reopen that ticket, but the system won't let me, and if I just comment on a closed ticket it'll probably be lost in the noise.)

The rationale for closing SERVER-8512 is incorrect. addToSet isn't a set operation, despite its name; it's an array operation. Code depends on it adding to the end and preserving order. If you want to add unordered set operations in the future, then that would be a different command than addToSet.

addToSet should support $slice, to allow adding to a list without allowing duplicates and limiting the total size of the list in the same operation. (Alternatively, to get the same effect, add an "ignoreDuplicates" boolean flag to $push and $pushAll to make it behave like addToSet, so its $slice modifier can be used.)

Comment by Scott Hernandez (Inactive) [ 29/Jan/14 ]

I believe this idea has been rejected for sets, but I pushed the idea before that arrays and set should have the same functionality in update.

Do you have a use-case where you want to sort/slice with a set?

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