代碼解釋
- 三張表:發(fā)布信息表v1_dailyReport、點贊表v1_zans、評論表v1_comments
- 關(guān)聯(lián)關(guān)系:點贊表和評論表中記錄的發(fā)布信息表的主鍵揖铜,reportId == _id
- 通過兩次lookup,關(guān)聯(lián)了點贊表和評論表
- 注意桃犬,最后需要return
// 按年月查詢發(fā)布信息锹锰,含點贊和評論
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)
}
}