all_course表结构:【课程基础表】
rel_stu_course表结构:【学生课程关联表】
需求:查询stuId:79550af260c865f11f9c9dde70e18f4c 对应的课程报名节数
用scrollview做了查询的分页弹窗,滚动下拉,页容量为100。预计一页就够了。。。。。。
写在了云函数里,pagelist:其中有多个页面的分页查询,有加条件的不加条件的,第一次写,也不知道如何优化结构,代码就冗余着能用就好了。。为啥分页都写在一个云函数里,因为用的免费版,免费版的云函数上限是十个。。。坑。。。我觉得就用一个云函数就行了,,当成controller,定义好统一的参数和返回值,加各种判断和逻辑。。。。。。。
total = (await db.collection('all_course').aggregate()
.lookup({
from: 'rel_stu_course',
let: {
course_name: '$name',
stuId:event.stuId,
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$name', '$$course_name']),
$.eq(['$stuId', '$$stuId']),
])))
.project({
_id: 0,
stuId: 1,
name:1,
count: 1,
})
.done(),
as: 'stus',
})
.count('count')
.end()).list[0].count;//总数
list = (await db.collection('all_course').aggregate()
.lookup({
from: 'rel_stu_course',
let: {
course_name: '$name',
stuId:event.stuId,
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$name', '$$course_name']),
$.eq(['$stuId', '$$stuId']),
])))
.project({
_id: 0,
stuId: 1,
name:1,
count: 1,
})
.done(),
as: 'stus',
})
.skip((event.page - 1) * event.pageSize)
.limit(event.pageSize)
.end()).list
小程序lookup代表left join,
pipeline发现可以筛选右表rel_stu_course的数据,网上查询到的都是pipeline里条件都是主表的条件 即all_course表,又看了一遍小程序的示例,根据最后一个lookup的给每个记录关联上子查询即相同的数据条目,改了改适用,完美解决右表加条件的需求!!!
官方示例链接:滑到最下面的例子【拼接被连接集合的子查询】稍微改改就好了