我有很多文章都有一个称为标签的字段,并且是标签_id的数组,出于统计目的,我想计算每个标签有多少篇文章。如果标记是一个简单的标记_id,这很容易,因为我可以按标记分组,但它是标记数组,并且不能按该字段分组。
首先,我尝试一下:
db.note.aggregate([{$match: {
publishedAt: {
$gte: ISODate('2018-01-01'),$lte: ISODate('2019-01-01')
}
}},{$group: {
_id: "$tags","total": {
"$sum": 1
}
}},{$lookup: {
from: 'tags',localField: '_id',foreignField: '_id',as: 'tag'
}},{$unwind: {
path: "$tag"
}},{$project: {
total: 1,"tag.name": 1
}},{$sort: {
total: -1
}}])
但这不起作用,该查询按标签分组,所以我尝试这样做:
{
'$match': {
'publishedAt': {
'$gte': new Date(req.body.gte),'$lte': new Date(req.body.lte)
}
}
},{
'$unwind': {
'path': '$tags'
}
},{
'$group': {
'_id': '$tags','total': {
'$sum': 1
}
}
},{
'$lookup': {
'from': 'tags','localField': '_id','foreignField': '_id','as': 'tag'
}
},{
'$project': {
'total': 1,'tag.name': 1
}
},{
'$sort': {
'total': -1
}
},{
'$unwind': {
'path': '$tag'
}
}
)
但是问题是,该组是数组中第一个标签,而我错过了该数组中的所有其他标签。
您认为解决方案是什么?