【发布时间】:2016-05-12 06:40:38
【问题描述】:
使用 MongoDB 的聚合管道,我希望能够按值对文档进行分组,而无需任何累积。
例如,我想按key对这个集合进行分组:
[
{
"_id":"323232",
"name":"Something",
"key":"A",
"price":"100"
},
{
"_id":"157236",
"name":"Another thing",
"key":"B",
"price":75
},
{
"_id":"555232",
"name":"Something or another",
"key":"B",
"price":78
}
]
想要的结果:
[
{
"_id":"A",
"results": [
{
"_id":"323232",
"name":"Something",
"key":"A",
"price":"100"
}
]
},
{
"_id":"A",
"results": [
{
"_id":"157236",
"name":"Another thing",
"key":"B",
"price":75
},
{
"_id":"555232",
"name":"Something or another",
"key":"B",
"price":78
}
]
}
]
【问题讨论】:
标签: mongodb mongodb-query