[SERVER-6074] Allow $slice operator in $project Created: 12/Jun/12 Updated: 06/Sep/17 Resolved: 14/Jul/15 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | 2.1.1 |
| Fix Version/s: | 3.1.6 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Matt Weyant | Assignee: | Charlie Swanson |
| Resolution: | Done | Votes: | 58 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||
| Backwards Compatibility: | Minor Change | ||||||||||||||||||||||||||||
| Sprint: | Quint Iteration 4, Quint Iteration 5, Quint Iteration 6 | ||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||
| Description |
|
Aggregation $project stage should support $slice operator on array fields similar to $slice in projection and syntax similar to aggregation $substr operator:
The above would take 4 elements from oldarray starting at position 10 (akin to "skipping first ten elements, as position is 0 based). Original description: If I have a document with an array of values in it, I would like to be able to project a slice of those values while aggregating.
$project with a $slice should work as expected:
results in a pipelined document =>
Similarly, the remainder of the existing $slice syntax should work as expected.
should produce
|
| Comments |
| Comment by Charlie Swanson [ 14/Jul/15 ] | ||||||||||||
|
The new operator is called $slice. It takes either 2 or 3 arguments. In the two argument form, the first argument is the array to slice, and the second argument is the count of the number of elements to slice (positive or negative). Here are some examples of the two argument form:
In the three argument form, the first argument is the array to slice, the second argument is the index from which to start the slice, and the third argument is the count of the number of elements to slice. Here are some examples of the three argument form:
See the test in the commit for more details. | ||||||||||||
| Comment by Githook User [ 14/Jul/15 ] | ||||||||||||
|
Author: {u'username': u'cswanson310', u'name': u'Charlie Swanson', u'email': u'charlie.swanson@mongodb.com'}Message: | ||||||||||||
| Comment by Ramon Fernandez Marina [ 04/Jun/15 ] | ||||||||||||
|
All, as you can see in the "Fix Version/s" field this functionality has been scheduled for the upcoming development release 3.1.4, due in the coming weeks. At the time of this writing this feature is in code review, so we expect to be able to merge the code into master very soon. Regards, | ||||||||||||
| Comment by yogesh [ 04/Jun/15 ] | ||||||||||||
|
Any one found solution regarding this ? | ||||||||||||
| Comment by Sam Verschueren [X] [ 16/Mar/15 ] | ||||||||||||
|
Is there a workaround for this issue? I have a lot of data and I only need the 10 first elements in the array when I aggregate. | ||||||||||||
| Comment by Asya Kamsky [ 01/Feb/15 ] | ||||||||||||
|
newmanw we are aware of the desirability of this feature - we are definitely considering it (or possibly one of several similar requests that have some overlap - | ||||||||||||
| Comment by William Newman [ 31/Jan/15 ] | ||||||||||||
|
Gentle bump to ask if there are plans to implement this. Possible $limit on $push might help as well. | ||||||||||||
| Comment by Roman [ 05/Nov/14 ] | ||||||||||||
|
Very strange that this Feature is not implemented yet!! | ||||||||||||
| Comment by Antonio Quintana [ 15/Apr/14 ] | ||||||||||||
|
My use case: I'm trying to get the top (and bottom) N cities, sorted by population and grouped by year. After some $sorts's, $group's and $push's, I've got something like this: {_id: "2013", Now, I want to do perform {$project: {cities: {$slice: N}}} I think the $project operator should accept all of the operators the projection document does in the find() command | ||||||||||||
| Comment by Pinte [ 10/Apr/14 ] | ||||||||||||
|
need it too | ||||||||||||
| Comment by Sergey Smirnov [ 13/Feb/14 ] | ||||||||||||
|
It's really necessarry thing! | ||||||||||||
| Comment by William Newman [ 11/Oct/13 ] | ||||||||||||
|
Any good work arounds for this issue? Really don't want to pull back millions of array values into memory to do the slice there. And map reduce is too slow. |