Help with mongodb query on an array field

Hi everyone !!

There is a query I’v been on for a long time now and I need help, if you’d be pleased !

In my documents there is a field like this :

jobs:[
{
       type:'Dev',
       nbRequired:2,
       collabs:['name1','name2','name3']
},
{
       type:'Front',
       nbRequired:2,
       collabs:['name1','name2','name3']
},
]

At the end I’d like to get an array like this to be used in my js script:

collab:[
       {type:'dev',name:'name1'},
       {type:'dev',name:'name2'},
       {type:'dev',name:'name3'},
       {type:'Front',name:'name1'},
       {type:'Front',name:'name2'},
       {type:'Front',name:'name3'}
]

Does anybody have an idea of what the best query would be ?

Thanks a lot !!

Hello @Preney_Valere, welcome to the MongoDB Community forum!

To get the required result you need to write an Aggregation query. I see that you can use the aggregation stages $unwind and $group. In addition, take a look at the Array Expression Operators used in aggregation queries.