【发布时间】:2020-10-09 20:56:48
【问题描述】:
我需要对按标签分组的记录进行计数,并在包括 in 个之前进行过滤
// in db
{tags: ['video', 'Alex'], ... },
{tags: ['video', 'John'], ... },
{tags: ['video', 'John'], ... },
{tags: ['text', 'Alex'], ... },
{tags: ['text', 'John'], ... },
client.db('mydb').collection('Files').aggregate(
[
{ $group: { _id: { tags: '$tags' }, total: { $sum: 1 } } },
{ $match: { tags: 'video' } },
],
).toArray()
但遗憾的是,我得到了零个文档。如果删除 $group 部分,我会得到 3 个文档。 在最初的请求中,我预计有 2 个文档
{ _id: ['video', 'Alex'], total: 1 },
{ _id: ['video', 'John'], total: 2 }
【问题讨论】:
标签: javascript node.js mongodb typescript