I'm kinda new in MongoDB and I'm having trouble to achieve a certain task. I'll try to explain it.
Here is my collection:
'workers': [
{
_id: ObjectId(...),
name: 'John',
skills: [
{ id: ObjectId('111'), value: 2 },
{ id: ObjectId('222'), value: 3 },
{ id: ObjectId('333'), value: 4 },
]
},
{
_id: ObjectId(...),
name: 'Mary',
skills: [
{ id: ObjectId('222'), value: 5 },
{ id: ObjectId('333'), value: 2 },
{ id: ObjectId('444'), value: 3 },
]
},
...
]
What I'm trying to achieve is something like this:
result: [
allSkills: [
{ id: ObjectId('111'), value: 2 },
{ id: ObjectId('222'), value: 5 },
{ id: ObjectId('333'), value: 4 },
{ id: ObjectId('444'), value: 3 }
]
]
My result should be a single array with all skills inside the workers collection (eventually I'll filter them).
In the example, I show my final result, with only unique values and the greater value for each skill, but what I'm trying to achieve here is just merge all skills arrays into a single allSkills array. For now, what I'm getting is a list of documents, each with its skills array.
Thanks in advance.