I have a collection data in MongoDB which looks like this -
document 1-
{
    metadata:[
        {
            "title": "High",
            "val": 12
        },
        {
            "title": "Medium",
            "val": 15
        },
        {
            "title": "Low",
            "val": 2
        }
    ]
}
document2 -
{
    metadata:[
        {
            "title": "High",
            "val": 10
        },
        {
            "title": "Medium",
            "val": 12
        },
        {
            "title": "Low",
            "val": 20
        }
    ]
}
& so on..
I wish to aggregate the val field based on the title value.
The output should look like -
{
    "High": 22,
    "Medium": 27,
    "Low": 22
}
How do I achieve this? Thanks in advance.
 
                        
$unwinddeconstructmetadataarray$grpupbytitleand make sum ofval$groupby null, convert array to object from key and value by$arrayToObject, and merge objects using$mergeObjectsPlayground