[SERVER-20163] aggregation: Should have a $zip operator to combine arrays Created: 27/Aug/15  Updated: 22/Mar/17  Resolved: 25/Mar/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 3.3.4

Type: New Feature Priority: Minor - P4
Reporter: Craig Wilson Assignee: Benjamin Murphy
Resolution: Done Votes: 0
Labels: expression
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CSHARP-1620 aggregation: Should have a $zip opera... Closed
Documented
is documented by DOCS-8821 3.4 agg operator $zip Closed
Duplicate
is duplicated by SERVER-17960 allow parallel $map processing for mu... Closed
Related
related to DRIVERS-297 Aggregation Framework Support for 3.4 Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 11 (03/14/16), Query 12 (04/04/16)
Participants:

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


 Comments   
Comment by Benjamin Murphy [ 25/Mar/16 ]

This ticket adds the $zip operator to aggregation, with syntax described in the description. Drivers that support aggregation pipeline helpers will need to be updated to support this expression.

Comment by Githook User [ 25/Mar/16 ]

Author:

{u'username': u'benjaminmurphy', u'name': u'Benjamin Murphy', u'email': u'benjamin_murphy@me.com'}

Message: SERVER-20163 Aggregation now supports the zip expression.
Branch: master
https://github.com/mongodb/mongo/commit/60f636afea30271a7393e05838378e9eeffc6806

Comment by Charlie Swanson [ 11/Mar/16 ]

After some internal discussion, I've updated the description to match the agreed-upon design. Will re-open this ticket and close the parallel $map ticket as a duplicate of this.

Comment by Charlie Swanson [ 04/Feb/16 ]

Closing this as a duplicate of SERVER-18115, since that one is more general. For example (inventing some syntax that may or may not match how this is eventually implemented), you could implement $zip by doing:

{$map: {
    input: {
        elt1: "$array1",
        elt2: "$array2"
    },
    in: ["$elt1", "$elt2"]
}

Anyone should feel free to re-open if you think it's worth adding $zip as a shortcut for that syntax, but I would imagine most people would be happy with SERVER-18115.

Generated at Thu Feb 08 03:53:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.