$$lookup
Joins multiple arrays of objects by a dynamic condition (transformer) on each pair of matches
Usage​
{
"$$lookup": [ /* Main array of elements */ ],
"using": [ /* array */
{
"with": /* array */,
"as": /* string */,
"on": /* Transformer(##current,##index,##{as}) */,
},
...
],
"to": /* Transformer(##current,##{as},...) */
}
"$$lookup(<using>,[to]):{input}"
{
"$$lookup": [ /* values */ ],
"using": [
{ "with": [ /* values 2 */], "as": /* name 2 */, "on": /* Transformer */ },
{ "with": [ /* values 3 */], "as": /* name 3 */, "on": /* Transformer */ },
...
],
"to": /* Transformer */
}
Returns​
array
- of type of to
's result or the merge of both primary array's item and with
's item
Arguments​
Argument | Type | Values | Required / Default Value | Description |
---|---|---|---|---|
Primary | array | Yes | Main array of elements | |
using | array | Yes | Array of definitions of how to match other arrays to the main one | |
to | Transformer(##current ,##{as} ,... ) | null | Transformer to map each pair of elements to its value in the result array  Default behavior is to merge ##current with ##\{as} (append & overwrite)
| |
using[].with | array | Yes | Array of elements to match with | |
using[].as | string | Yes | The name the elements from this entry will be referred as (#\{as} ) | |
using[].on | Transformer(##current ,##index ,##{as} ) | Yes | Evaluated condition on when to match an element from the main array and with array (uses the Truthy logic)
|
Examples​
Input
Definition
Output
{
"a": [
{
"id": 2,
"a": 1
},
{
"id": 5,
"a": 2
}
],
"b": [
{
"id": 2,
"a": "x"
},
{
"id": 5,
"e": true
}
]
}
{
"$$lookup": "$.a",
"using": [
{
"with": "$.b",
"as": "match",
"on": {
"$$is": "##current.id",
"eq": "##match.id"
}
}
]
}
[
{
"id": 2,
"a": "x"
},
{
"id": 5,
"a": 2,
"e": true
}
]
{
"a": [
{
"id": 2,
"a": 1
},
{
"id": 5,
"a": 2
}
],
"b": [
{
"key": 2,
"a": "x"
},
{
"key": 5,
"e": true
}
]
}
{
"$$lookup": "$.a",
"using": [
{
"with": "$.b",
"as": "match",
"on": {
"$$is": "##current.id",
"eq": "##match.key"
}
}
]
}
[
{
"id": 2,
"a": "x",
"key": 2
},
{
"id": 5,
"a": 2,
"e": true,
"key": 5
}
]
{
"a": [
{
"id": 2,
"a": 1
},
{
"id": 5,
"a": 2
}
],
"b": [
{
"id": 2,
"a": "x"
},
{
"id": 5,
"e": true
}
]
}
{
"$$lookup": "$.a",
"using": [
{
"with": "$.b",
"as": "match",
"on": {
"$$is": "##current.id",
"eq": "##match.id"
}
}
],
"to": {
"*": "##current",
"e": "##match.e"
}
}
[
{
"id": 2,
"a": 1
},
{
"id": 5,
"a": 2,
"e": true
}
]
{
"a1": [
{
"id": "aaa",
"val": "a"
},
{
"id": "bbb",
"val": "b"
}
],
"a2": [
{
"name": "aaa",
"val": "A"
},
{
"name": "bbb",
"val": "B"
}
]
}
{
"$$lookup": "$.a1",
"using": [
{
"with": "$.a2",
"as": "a2",
"on": {
"$$is": "##current.id",
"eq": "##a2.name"
}
}
],
"to": [
"##current.val",
"##a2.val"
]
}
[
[
"a",
"A"
],
[
"b",
"B"
]
]