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

aggregation: Should have a $zip operator to combine arrays

    XMLWordPrintable

    Details

      Description

      Syntax

      {
          $zip: {
              inputs: <array of expressions>
              defaults: <array of expressions>   
              useLongestLength: <bool - default false, cannot specify defaults if false>
          }
      }
      

      Example

      Input

      {_id: 0, evens: [0, 2, 4, 6], odds: [1, 3, 5]}
      {_id: 1, evens: [4], odds: [1, 3]}
      

      Pipeline

      db.coll.aggregate([{
          $project: {
              z: {
                  $zip: {
                      inputs: ["$evens", "$odds"],
                      defaults: [0, 1],
                      useLongestLength: true
                  }
              }
          }
      }])
      

      Output

      {_id: 0, z: [[0, 1], [2, 3], [4, 5], [6, 1]]}
      {_id: 1, z: [[4, 1], [0, 3]]}
      

      Additional Notes

      • Uses length of shortest array, unless useLongestLength is true. If useLongestLength is true, but defaults is not specified, will use null to fill in.
      • The use of null here is consistent with our parsing of array literals (e.g. [0, "$field"] will become [0, null] if 'field' does not exist).
      • 'defaults' cannot be specified unless useLongestLength is true.
      • If any input is null, will return null. $map returns null if its input is nullish.
      • Will error if any input is not an array (and is not nullish).
      • Will error if 'defaults' is specified without 'useLongestLength', or is not the same length as 'inputs'.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              benjamin.murphy Benjamin Murphy
              Reporter:
              craiggwilson Craig Wilson
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: