[SERVER-2362] Add new Deque/Set operation modifiers Created: 15/Jan/11  Updated: 06/Apr/23  Resolved: 29/Jun/19

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

Type: New Feature Priority: Minor - P4
Reporter: Keith Branton Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 13
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-6399 Refactor update() code Closed
is depended on by SERVER-2363 $push/$pushAll support for insertion ... Closed
Related
related to SERVER-9591 Update: $push + $unique Closed
related to SERVER-1050 not allowed to $push and $pop to same... Closed
Assigned Teams:
Query
Participants:

 Description   

$unshift - like $push, but to the start of the array, and with support for the $each construct like $addToSet.

$pushToSet - like $push in that the supplied item(s) will end up at the end of the set in the supplied order - maintaining set semantics by removing pre-existing matching elements. Should support the $each construct.

$unshiftToSet - just like $pushToSet, but adds items at the start of the array.

The next two are more "nice to have's".

$push - add support for the $each construct - $pushAll could then be deprecated

$shift - just like $pop with reversed sign, i.e. {$pop:{a:-1}} is the same as {$shift:{a:1}}



 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.

This can be done by setting the array to its new value using various aggregation array expressions. I believe this can handle all of the examples/use cases that were mentioned in the comments. In particular, $size, $slice, and set operators would be useful.

Comment by Dan Dascalescu [ 15/Aug/15 ]

I would like to see $unshift implemented simply as an alias to $position: 0, but without requiring $each, which doesn't read intuitively.

Filed a separate ticket at SERVER-19974.

Comment by Kevin J. Rice [ 03/Apr/13 ]

Echo TP Diffenbach's comment. This is EXACTLY what we're doing now, storing data in an array:

ts=timestamp, val=value

vals : [ [ts1,val1], [ts2,val2], [ts3,val3], ... ]

Every minute, I'm adding new data with $push, onto the end of the array.

But, to keep record sizes at 4k, I have splitRecords.py. It moves off the oldest 125 datapoints to their own document (all during which time a new value may come in while I'm busy pulling this data out). So, I'm pulling from the front of the array. I only get the option of one element at a time. I'd like to pull an arbitrary number of elements from the front of the array.

My temporary, and ugly, workaround is to use $pullAll and reference the exact elements to be pulled. I'm very lucky in that my data is unique and I can do this. But, it's ugly - I only need to pull n elements off the front of the array, not search for them individually and remove each specially.

Comment by TP Diffenbach [ 14/Mar/13 ]

Here's my use case, and I think it's compelling.

When I have an array of history, I want to be able to distinguish one item as the current value. Whether that's the zeroth or the last element doesn't much matter.

Given db.zeroth.insert( { x:1, statusHistory: [

{ status : "approved"}

,

{ status : "draft"}

,

{status : "created"}

]}),

I can reference the zeroth element as the "current" value:

db.zeroth.find(

{ "statusHistory.0.status" : "approved" }

); // find all currently in approved status

So I'm storing the array newest to oldest so that I can reference the newest (current) status at element zero.

But now I can't easily add a newer status, because it would be pushed to the end of the array.

If instead I store the array oldest to newest, I can easily push newer statuses, but I can't easily reference the newest.

The Mongo solution to issues like this with arrays this seems to be "store it (special element, array length, whatever) explicitly as another attribute or sub-document". But that ends up either duplicating data, or fracturing it into a current part and a history part, and it doesn't play well with the New Aggregation Framework (e.g., if I wanted to $unwind all status, including the current).

But unless I duplicate data, I have no way to be able to query and update the same position: with one array, I can query element zero, or I can push onto the end, but there's no position that I can explicitly both query and update.

Comment by Ben Brockway [ 11/Sep/12 ]

Being able to insert an item at the start of an array would be very useful for me for some applications I will be developing over the next few years

Comment by Chris Hanks [ 30/Apr/11 ]

These would be great - the ability to use $pushToSet on a fixed-size array would really help me with a problem I'm having.

Comment by Eliot Horowitz (Inactive) [ 16/Jan/11 ]

Not sure if both will happen or one, but marked as related .

Comment by Keith Branton [ 15/Jan/11 ]

Scott Hernandez has pointed out that $pushToSet could be performed with a combination of $pull(All) and $push(All) once http://jira.mongodb.org/browse/SERVER-1050 is implemented.

On further reflection I currently have no use-cases for "pushing" to both ends of any single array, so the $unshift operations could be worked around by storing all the relevant arrays backwards. Presumably that would perform slightly better too since fewer items would need to be moved when a $push happens than with $unshift.

I still think this proposal is more intuitive, though, and all my user-sorted arrays are pretty small so I doubt I'd notice the performance difference. Seeing the same order in the shell as my application uses is more obvious when debugging, so if it's not to difficult to do I'm still asking

Comment by Keith Branton [ 15/Jan/11 ]

$pushToSet would be a key component in solving the use-case discussed on this thread: http://groups.google.com/group/mongodb-user/browse_thread/thread/b62bea001b9f81ea#

The user wants to track the last 10 users to view a given profile - so he needs capped arrays http://jira.mongodb.org/browse/SERVER-991 but also need the most recent visitor to always be at the end of the array in a way that $addToSet doesn't do.

This interests me because it is also a feature I plan to implement on my web site - very common social networking use-case.

Comment by Keith Branton [ 15/Jan/11 ]

$unshiftToSet would be really useful to me. My application allows users to change the order of lists, usually represented as an array of FKs in mongo. I currently replace the whole document with optimistic locking semantics to achieve this, but I would much rather use modifiers.

The complication here is concurrent modifications. If user A reads [1,2,3,4,5] and changes it to [1,3,5,2,4] but in the meantime user B has changed the array to [7,1,2,6,4,3], then I would want the array to look like [1,3,5,2,4,7,6] after user A's changes are saved.

With $unshiftToSet I could use an update with {$unshiftToSet:{a:{$each:[1,3,5,2,4]}}} to achieve this.

Generated at Thu Feb 08 02:59:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.