I have these collections
purchaseorder
{
"_id": "5f0104a8d0c3a06fc9c06625",
"purchaseItems": [
{
"product": "5eff29e9e2708a0ca980762e",
"quantity": 1
},
{
"product": "5eff29e9e2708a0ca980762e",
"quantity": 2
}
],
"totalQuantity": 0,
"documentDate": {
"$date": "2020-07-04T16:00:00.000Z"
}
}
and
product
{
"_id": "5eff29e9e2708a0ca980762e",
"name": "name",
"code": "code",
"cost": "1",
"srp": "1",
"minimum": "1",
"startEffectiveDate": {
"$date": "2020-07-04T16:00:00.000Z"
}
}
How do I join the product field inside the purchaseItems which is an Embedded Array Document to the product collection. I need something like this.
{
"_id": "5f0104a8d0c3a06fc9c06625",
"purchaseItems": [
{
"product": {
"_id": "5eff29e9e2708a0ca980762e",
"name": "product name"
},
"quantity": 1
},
{
"product": {
"_id": "5eff29e9e2708a0ca980762e",
"name": "product name 2"
},
"quantity": 1
}
],
"totalQuantity": 0,
"documentDate": {
"$date": "2020-07-04T16:00:00.000Z"
}
}
I tried using this aggregation but I got an incorrect result
[{$lookup: {
from: 'product',
'let': {
purchaseItems: '$purchaseItems'
},
pipeline: [
{
$lookup: {
from: 'product',
'let': {
product: '$product'
},
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$product" ] } } }
],
"as": "product"
}
}
],
as: 'purchaseItems'
} }]
Not really familiar with mongodb and I'm already considering going back to SQL.