[SERVER-130] array $pull operator Created: 06/Jul/09 Updated: 12/Jul/16 Resolved: 22/Jul/09 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Usability |
| Affects Version/s: | None |
| Fix Version/s: | 0.9.7 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Eliot Horowitz (Inactive) | Assignee: | Aaron Staple |
| Resolution: | Done | Votes: | 4 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Participants: |
| Description |
|
I believe adding a $pull modifier - the opposite of $push - would be a For example: db.group.insert({_id:1,text:'user group 1',users:[1,2,3]}); // add another user - atomic } ); // remove a user - atomic }); |
| Comments |
| Comment by Ivan Schütz [ 27/Sep/13 ] |
|
Hello, @sandstrom: I found a case where this is not true. I'm storing some aggregated values (like averages) from certain fields of the array's elements in the containing document (for performance). When I remove an element from the array, I have to recalculate the aggregated values. In this case it's useful if I get the removed element. Otherwise I have to do 2 queries (retrieve the element and pull). Besides of that I also could imagine cases where at point of deleting an item, only the id is known. After delete I want to send a confirmation to the user about the deleted item, maybe with name or something. It would be easy if pull returned the removed element, otherwise again 2nd query is necessary. This is written a few years later but right now I'm missing this functionality. Wonder why nobody else has mentioned this. |
| Comment by Aaron Staple [ 28/Jul/09 ] |
|
Hi Wouter, I wrote $pull to remove all matching elements because arrays are often used to store sets of values, and set logic is used when matching an array. You're the one with a use case though, so let us know what functionality makes the most sense for you. Do you also want a 'pop' modifier that doesn't accept a value argument? Aaron |
| Comment by Wouter [ 24/Jul/09 ] |
|
What is the expected behaviour of $pull?! Now, if the array contains a value several times, all of them are removed if you run $pull. {_id:1,arr : [1,2,2,2]} update({_id:1},{$pull:{arr:2}}); {_id:1,arr : [1]} Not sure if this is good or bad - just wanted to let this know. BTW array_pop is different, array_pop is run on the array, you don't specify the value you want to be removed, so it's a good thing the removed value is returned (Compare: $removed_item = array_pop($array); vs $pull : { array_name : $removed_item }) |
| Comment by Aaron Staple [ 22/Jul/09 ] |
|
Ok, I've gone ahead and implemented $pull, tested in pull.js. Let me know if you have any difficulties. |
| Comment by sandstrom [ 20/Jul/09 ] |
|
I agree with Wouter. No need to return the pulled item. I really dislike how PHP's pop returns the popped value, it's completely useless. Since virtual collections will eliminate the need for push/pull it might be better to spend the time on implementing them. http://jira.mongodb.org/browse/SERVER-142 |
| Comment by Wouter [ 07/Jul/09 ] |
|
@Dwight: Well, you specify the value(s) that you want to remove from the array. If you want to know what values are actually in the array, you can simply read the object. If I run }); I do not need to know if the value '2' was in the array. I just want to be sure it is not in the array after the update |
| Comment by Dwight Merriman [ 06/Jul/09 ] |
|
seems to me oine would want the popped (pulled) item returned. not sure best way to do that. |