【发布时间】:2017-08-08 20:18:31
【问题描述】:
目前管道的结果:
[
{
catId: 'A1',
categories: [
{
name: 'Monitors',
section: 99
},
{
name: 'Monitors',
section: 99
},
{
name: 'Monitors',
section: 98
}
]
}
]
我已经将它按猫 ID 分组并使用 $push 来填充类别数组。
我还想对类别数组进行分组
[
{
catId: 'A1',
categories: [
{
name: 'Monitors',
section: 99,
count: 2
}
{
name: 'Monitors',
section: 98,
count: 1
}
]
}
]
我还没有找到任何关于如何在数组上执行 $group 的示例。
到目前为止我的查询:
let query = mongoose.model('categories').aggregate([
{
$group: {
_id: {catId: '$catId'},
categories: {$push: {name: "$name", section: "$section"}}
}
},
{
$project: {
catId: '$_id.catId',
categories: 1,
_id: 0,
}
}
]);
【问题讨论】:
-
请添加您当前的查询
-
有很多方法,但如果你真的想基于目前现有的聚合来做到这一点,那么最有意义的是向你展示现有的管道和源文档。因为这些方法中“最有效”的方法是在达到这一阶段的结果之前,实际上首先考虑添加这些总数和聚合级别。
-
用查询更新了问题。
标签: mongodb aggregation-framework