Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1620

aggregation: Should have a $zip operator to combine arrays

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4
    • Affects Version/s: None
    • Component/s: Linq
    • Labels:
      None

      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:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            rathi.gnanasekaran Rathi Gnanasekaran
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: