Skip to main content

$$lookup

Joins multiple arrays of objects by a dynamic condition (transformer) on each pair of matches

Usage​

{
"$$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
PrimaryarrayArray of objectsYesArray of elements
usingarrayArray of UsingEntrysYesArray of defitinitions of how to match other arrays to the main one
toTransformer(##current,##{as-1},...)Input is ##current (current element) & ##{as-1} (matched element from with)Merge ##current with ##match (Append & Overwrite)Transformer to map each pair of elements to its value in the result array

UsingEntity​

ArgumentTypeValuesRequired / Default ValueDescription
witharrayArray of objectsYesArray of elements to match with
asstringYesThe name the elements from this entry will be referred as
onTransformer(##current,##index,##{as})Input is ##current (current element), ##index (current index) & ##{as} the matched element from the array (replace {as} with the name provided in as argument)YesEvaluated 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"
]
]