I have the below schema
{
_id: ObjectId,
account: [{
accountId: String,
commonId: string
}
],
sessions: [{
sessionId: String,
commonId: String
}
]
}
I want to combine both these array into one array of objects in aggregate query, where the result should be
{
_id: ObjectId,
merged: [{
accountId: String,
stringId: String,
commonId: string
}
],
}
Sample document:
{
_id: 0,
account: [
{
accountId: '1234',
commonId: '0'
},
{
accountId: '1235',
commonId: '1'
},
{
accountId: '1236',
commonId: '2'
}
],
sessions: [
{
sessionId: '6781',
commonId:'0'
},
{
sessionId: '6782',
commonId:'1'
},
{
sessionId: '6783',
commonId:'2'
}
]
expected output:
{
_id: 0,
merged: [
{
accountId: '1234',
sessionId: '6781',
commonId: '0'
},
{
accountId: '1235',
sessionId: '6782',
commonId: '1'
},
{
accountId: '1236',
sessionId: '6783',
commonId: '2'
}
],
this or unwinded version of this
How can i do this? Any help is appreciated
accountandsessionswill always have the same size / order? Or we need to checkcommonIdto merge items?