I want to use $lookup in the aggregate method.
The collection I want to do this is like this. It is an array. It has nested objects in the array.
Please notice my problem is with the lookup and the way I want to get my data. The names of the properties are not so important, the important property is "position_id".
[
{
name : 'projectName1',
positions : [
{
position_id : id1,
use : true,
wage : 0,
default : true
},
{
position_id : id2,
use : true,
wage : 0,
default : true
}
]
}
]
When I lookup on the "position_id" like this:
$lookup: {
from: 'positions',
localField: 'positions.position_id',
foreignField: '_id',
as: 'positions.positionDetails'
}
The result is:
"positions": {
"positionDetails": [
{
"_id": "63d78e5096109914dc963431",
"field": "electrical",
"regularName": "elc",
},
{
"_id": "63d78e5096109914dc963433",
"field": "mechanic",
"regularName": "mec",
}
]
}
The positions array was changed to an object.
But I want to get data like this :
"positions": [
{
"position_id": "63d78e5096109914dc963431",
"use": true,
"default": true,
"wage": 0,
"positionDetails" : {
"field": "electrical",
"regularName": "elc",
}
},
{
"position_id": "63d78e5096109914dc963433",
"use": true,
"default": false,
"wage": 0,
"positionDetails" : {
"field": "mechanic",
"regularName": "mec",
}
}
]