In mongodb, I have a get object in below shape after running aggregation.
{
"_id": 1,
"specificationList": {
"key": "Memory & Storage Features",
"values": [
{
"key": "Internal Storage",
"value": [
"32 KB"
]
},
{
"key": "RAM",
"value": [
"32 MB"
]
},
{
"key": "Expandable Storage",
"value": [
"8 GB"
]
},
{
"key": "Supported Memory Card Type",
"value": [
"MicroSD"
]
}
]
}
}
From above document , how could I get object in below shape , in next aggregation pipeline. I need to get to below shape so as to make code cleaner. I am using aggregation to arrive at above shape (so want to append another pipeline), and it would be nice to know what aggregation pipeline to get at below shape
{
"specList” : {
“Internal Storage”: “32 KB”,
“RAM”:”32 MB”,
“Expandable Storage”:”8 GB”,
“Supported Memory Card Type”:”MicroSD”
}
}
{ "k": "RAM", "v": "32 MB" }instead of{ "key": "RAM", "value": ["32 MB"] }then you can simply apply the$arrayToObjectoperator within a$projectstep as your final pipeline stage otherwise you would need to map the values to the above recommended shape, like in the answer I provided below.