Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-6074

Allow $slice operator in $project

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major - P3
    • Resolution: Done
    • 2.1.1
    • 3.1.6
    • Aggregation Framework
    • None
    • Minor Change
    • Quint Iteration 4, Quint Iteration 5, Quint Iteration 6

    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 }
        ]
      }
      

      Attachments

        Issue Links

          Activity

            People

              Votes:
              58 Vote for this issue
              Watchers:
              47 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: