【发布时间】:2017-10-24 02:19:35
【问题描述】:
我有两个系列; users 和 communities。我的用户可以链接到许多社区,因此我将 linkedCommunities 作为 User 架构中的数组,如下所示:
const userSchema = new Schema({
linkedCommunities: [
{
type: mongoose.Schema.ObjectId,
ref: 'Community'
}
]
});
在我的communities 视图中,我想显示链接到每个社区的users 的数量,因此它会显示为:
| Community | Number of users |
| --------- | --------------- |
| Community 1 | 45 |
| Community 2 | 78 |
| Community 4 | 107 |
理想情况下,当我在 Community 模型上进行查找时,我想要一个名为 linkedUserCount 之类的字段。
我确信我可以使用aggregate 以某种方式实现这一目标,但我正在努力寻找合适的管道运营商和订单。
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query aggregation-framework