代码解释
- 三张表:发布信息表v1_dailyReport、点赞表v1_zans、评论表v1_comments
- 关联关系:点赞表和评论表中记录的发布信息表的主键,reportId == _id
- 通过两次lookup,关联了点赞表和评论表
- 注意,最后需要return
// 按年月查询发布信息,含点赞和评论
async function searchDailyReportIncloudZansComments(event, wxContext) {
try {
return await db.collection('v1_dailyReport').aggregate()
.lookup({
from: 'v1_zans',
localField: '_id',
foreignField: 'reportId',
as: 'zansList',
})
.lookup({
from: 'v1_comments',
localField: '_id',
foreignField: 'reportId',
as: 'commentsList',
})
.match({
classId: event.classId,
certainYear: event.certainYear,
certainMonth: event.certainMonth,
dataStatus: 1
})
.sort({
'issueDate': -1,
'createDate': -1
})
.limit(1000)
.end()
.then(res => {
// console.log('res', res)
return res
})
} catch (e) {
console.error('error', e)
}
}