小程序云函数库的多表查询

时间:2021-03-15 13:21:54   收藏:0   阅读:39

// 云函数入口文件
const cloud = require(‘wx-server-sdk‘)

cloud.init()

const db = cloud.database()
exports.main = async (event, context) => {
try{
return await db.collection("tb_user").aggregate().lookup({//tb_user是主表,from是副表。
from:"tb_class",
localField: ‘user_classes‘,//这是主表中的关联字段
foreignField: ‘_id‘,//这是副表中的关联字段
as: ‘classInfo‘//这个方法就是说,把查询的副表数据当成这样一个数组,与主表数据一起查出来
}).match({
_id:event._id//主表查询的字段
}).limit(1000).end().then(res=>{
console.log(res)
return res
})
}catch(err){
console.error(err)
}
}

原文:https://www.cnblogs.com/lcxtm/p/14536732.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!