【发布时间】:2021-05-19 02:53:39
【问题描述】:
我掌握的数据是:
[
{ type: 'software' },
{ type: 'hardware' },
{ type: 'software' },
{ type: 'network' },
{ type: 'test' },
...
]
我想通过聚合管道创建一个 MongoDB 组以返回如下数据: 我只想要结果中的 3 个对象 结果中的第三个对象 {_id: 'other', count: 2}, 这应该是软件和硬件以外类型的计数总和
[
{_id: 'software', count: 2},
{_id: 'hardware', count: 1},
{_id: 'other', count: 2},
]
【问题讨论】:
-
这些数据是存储在单个文档的字段中,还是存储在单独的文档中?
-
@AlexZeDim 这些是单独的文档。
-
您可能想通过按评分下方的绿色标记来检查并接受我的答案(如果我的答案正确,可能还会投票)
-
我更新了我的查询,您可以再次查看结果。
标签: node.js mongodb group-by pipeline aggregation