-
Type:
New Feature
-
Resolution: Done
-
Priority:
Minor - P4
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
Fully Compatible
-
Query 11 (03/14/16), Query 12 (04/04/16)
-
None
-
None
-
None
-
None
-
None
-
None
-
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'.
- is depended on by
-
CSHARP-1620 aggregation: Should have a $zip operator to combine arrays
-
- Closed
-
- is duplicated by
-
SERVER-17960 allow parallel $map processing for multiple arrays
-
- Closed
-
- related to
-
DRIVERS-297 Aggregation Framework Support for 3.4
-
- Closed
-