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

aggregation: Should have a $zip operator to combine arrays

    • Fully Compatible
    • Query 11 (03/14/16), Query 12 (04/04/16)

      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'.

            Assignee:
            benjamin.murphy Benjamin Murphy
            Reporter:
            craig.wilson@mongodb.com Craig Wilson
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: