Details

    • Type: New Feature
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: 2.1.1
    • Fix Version/s: 3.1.4
    • Component/s: Aggregation Framework
    • Labels:
      None

      Description

      Aggregation $project stage should support $slice operator on array fields similar to $slice in projection and syntax similar to aggregation $substr operator:

      { "$project" : { "newArray" : { "$slice" : [ "$oldarray" , 10, 4 ] } } }
      

      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.

      { title: "A doc",
        intervals: [
          { value: 1, foo: true, bar: false },
          { value: 2, foo: false, bar: true },
          { value: 3, foo: true, bar: false}
        ]
      }
      

      $project with a $slice should work as expected:

      { $project : {
          title: 1,
          fewerIntervals: {intervals:{$slice:2}}
        }
      }
      

      results in a pipelined document =>

      { title: "A doc",
        fewerIntervals: [
          { value: 1, foo: true, bar: false },
          { value: 2, foo: false, bar: true }
        ]
      }
      

      Similarly, the remainder of the existing $slice syntax should work as expected.

      { $project : {
          title: 1,
          fewerIntervals: {intervals:{$slice:[1,1]}}
        }
      }
      

      should produce

      { title: "A doc",
        fewerIntervals: [
          { value: 2, foo: false, bar: true }
        ]
      }
      

        Issue Links

          Activity

          Hide
          newmanw William Newman added a comment -

          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.

          Show
          newmanw William Newman added a comment - 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.
          Hide
          smilesrg Sergey Smirnov added a comment -

          It's really necessarry thing!

          Show
          smilesrg Sergey Smirnov added a comment - It's really necessarry thing!
          Hide
          fpinte Pinte added a comment -

          need it too

          Show
          fpinte Pinte added a comment - need it too
          Hide
          antonimmo Antonio Quintana added a comment -

          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",
          cities:["MEX", "CUN", "MIA", "JFK", "LHR", ...]
          },
          { _id: "2014",
          cities:["MEX", "MIA", "GCM", "JFK", "DFW", ...]]
          }
          .. and so on

          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

          Show
          antonimmo Antonio Quintana added a comment - 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", cities: ["MEX", "CUN", "MIA", "JFK", "LHR", ...] }, { _id: "2014", cities: ["MEX", "MIA", "GCM", "JFK", "DFW", ...] ] } .. and so on 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
          Hide
          rwander Roman added a comment -

          Very strange that this Feature is not implemented yet!!

          Show
          rwander Roman added a comment - Very strange that this Feature is not implemented yet!!
          Hide
          newmanw William Newman added a comment -

          Gentle bump to ask if there are plans to implement this. Possible $limit on $push might help as well.

          Show
          newmanw William Newman added a comment - Gentle bump to ask if there are plans to implement this. Possible $limit on $push might help as well.
          Hide
          asya Asya Kamsky added a comment -

          William Newman 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 - SERVER-4589 and SERVER-9377) for upcoming releases.

          Show
          asya Asya Kamsky added a comment - William Newman 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 - SERVER-4589 and SERVER-9377 ) for upcoming releases.
          Hide
          SamV Sam Verschueren added a comment -

          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.

          Show
          SamV Sam Verschueren added a comment - 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.

            People

            • Votes:
              52 Vote for this issue
              Watchers:
              41 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since reply:
                9 weeks, 5 days ago
                Date of 1st Reply:

                Agile