I have a few mongodb documents like this
{'_id':'1',
'year':2020,
'season':'Summer',
'status':'Over'},
{'_id':'2',
'year':2020,
'season':'Winter',
'status':'Pending'},
{'_id':'3',
'year':2020,
'season':'Rainy',
'status':'Pending'},
{'_id':'4',
'year':2021,
'season':'Summer',
'status':'Pending'}
........
...
.
If I give an input of array of years [2020,2021], then my expected output should be
[{year:2020,sesason:['Winter','Rainy']},{year:2021,sesason:['Summer','Winter','Rainy']}]
where season array should only contain values whose status is ‘Pending’.
I tried this code,but not getting the expected output
var arr =[];
var yearsArray = [2020,2021]
var seasonArray = ['Summer','Rainy','Winter']
var pendingSeasons = [];
return await Promise.all(
yearsArray.map(async(year) =>{
return await Promise.all(
seasonArray.map(async(tP) =>{
await db.collection('CC')
.find({'year':year,'season':tP})
.toArray()
.then((data) =>{
if(data.status != 'Over'){
pendingSeasons.push(tP)
}
})
})
).then(arr.push({"year":year,"season":pendingSeasons}))
})
).then(() =>{
return arr
})