在对集合中的数据进行分组后,我正在try 获取特定格式的输出.
集合中的数据如下所示:
[
{
"country": "UK",
"business": "computer",
"companies": 24
},
{
"country": "UK",
"business": "automotive",
"companies": 12
},
{
"country": "US",
"business": "medical",
"companies": 3
},
{
"country": "US",
"business": "food",
"companies": 53
}
]
我使用了以下聚合来根据country
对business
进行分组:
[
{
$group: {
_id: "$country",
business: {
$addToSet: "$business"
}
}
}
]
我得到了以下结果:
[
{
"_id": "UK",
"business": [ "computer", "automotive" ]
},
{
"_id": "US",
"business": [ "food", "medical" ]
}
]
我的问题是:如何使用管道并将输出格式化为如下内容?
[
{
"US": {
"business":[ "food", "medical" ]
}
},
{
"UK": {
"business":[ "computer", "automotive" ]
}
}
]