[SERVER-2191] $push() to front of array Created: 08/Dec/10  Updated: 07/Mar/14  Resolved: 01/Oct/13

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

Type: New Feature Priority: Major - P3
Reporter: Ed Rooth Assignee: Scott Hernandez (Inactive)
Resolution: Duplicate Votes: 8
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-2363 $push/$pushAll support for insertion ... Closed
Related
is related to SERVER-6399 Refactor update() code Closed
Participants:

 Description   

Currently $push() only pushes items onto the end of the array.
It would be nice if we could pass an optional boolean parameter that would allow pushing to the front of the array instead.

This would allow reversing the order of arrays by nature of the front-push update, and would eliminate the need to do any client side sorting or reversing.
Currently adding array items atomically in reverse chronological order is not possible.

This is similar to SERVER-2036, and having an $insert() would accomplish the same thing.
But more specific and easier.



 Comments   
Comment by D [ 06/Apr/12 ]

This was not a question of usage (of the positional operator), I know how to do this.

But a remark on how the positional operator becomes unsafe by the proposed new feature.

Comment by Scott Hernandez (Inactive) [ 04/Apr/12 ]

Your document is invalid and so is your update using "$". I think you want this:

 t.update( { '_id':'1', "values.A":"A" }, { $inc:{ 'values.$.num':2 }}, false, true ) 

where you have to use a query to find the array element you want to update using the positional operator.

Please post on http://groups.google.com/group/mongodb-user/ if you have usage questions.

Comment by D [ 04/Apr/12 ]

What if I have an embedded array like this:
{
"_id" : "1"
"values" : [
"A" :

{ "A": "A", "num" : 1 }

,
"B" :

{ "B": "B", "num" : 2 }

,
"C" :

{ "C": "C", "num" : 3 }

]
}

And I update one array element with the positional operator:
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

t.update(

{ '_id':'1' }

, { $inc:{ 'values.$.num':2 }}, false, true ).

But now someone has reordered in between fetch and update, the update will match the wrong element in the array.

So how are we supposed to update array elements safely when there is no support for natural Id's in mongo?

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