我正在尝试修改查询,因此它也会返回元素数 我想计算与“出版物”有关的“PublicationComments”的数量。现在查询看起来像这样:
let query = {
attributes: {
exclude: attributesToExclude,
},
where: {
'club_id': this.id,
},
order: [['date', 'DESC']],
include: [
{
model: models.PublicationComment,
association: models.Publication.hasMany(models.PublicationComment, { as: 'lastComments' }),
order: [['date', 'DESC']],
limit: 3,
include: [
{
model: models.User, as: 'user',
attributes: {
exclude: attributesToExclude,
},
}
],
},
{
model: models.PublicationLike,
association: models.Publication.hasMany(models.PublicationLike, { as: 'likes' }),
attributes: ['userId']
}
]
};
我想要获得的结果如下:
[ { id: '2',
clubId: '26',
teamId: null,
userId: '67',
date: '2017-04-18T11:23:05.628Z',
card:
{ type: 'status',
text: 'publication example text',
video: [Object] },
created_at: '2017-04-18T11:23:05.629Z',
updated_at: '2017-04-18T11:23:05.629Z',
deleted_at: null,
likes: [],
commentsCount: 4,
lastComments: [ [Object], [Object], [Object] ] }]
请注意,有一个CommentCount属性,其中包含所需计数查询的结果
我尝试通过注释添加包含属性计数功能,并重复与PublicationComment模型的关联,不带限制参数(我需要总计)。 像这样:
attributes: {
include: [[sequelize.fn('count', sequelize.col(allComments.id), 'commentsCount']]
exclude: attributesToExclude,
},
在icludde数组中添加新的associaton:
{
model: models.PublicationComment,
association: models.Publication.hasMany(models.PublicationComment, { as: 'allComments' })
}
但在经过两天的努力后,我无法使其发挥作用。
我是否有任何建议,资源或澄清可以用来推进?感谢。