Skip to main content

$$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​

ArgumentTypeValuesRequired / Default ValueDescription
PrimaryarrayYesMain array of elements
usingarrayYesArray of definitions of how to match other arrays to the main one
toTransformer(##current,##{as},...)nullTransformer to map each pair of elements to its value in the result array  Default behavior is to merge ##current with ##\{as} (append & overwrite)
  • ##current - Current element
  • ##{as} - Matched element from with
  • ... -
using[].witharrayYesArray of elements to match with
using[].asstringYesThe name the elements from this entry will be referred as (#\{as})
using[].onTransformer(##current,##index,##{as})YesEvaluated condition on when to match an element from the main array and with array (uses the Truthy logic)
  • ##current - Current element
  • ##index - Current index
  • ##{as} - the matched element from the array (replace \{as} with the name provided in as argument)

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"
]
]