目录
articleTest -- 文章表
article_operator -- 阅读、点赞、评论表
多表简单查询
return await db.collection('fzxk_course_info').aggregate()
.lookup({
from:'fzxk_exam',
localField: '_id',
foreignField: 'course_info_id',
as: 'examList',
})
.lookup({
from:'fzxk_course_section',
localField: '_id',
foreignField: 'course_info_id',
as: 'sectionList',
})
.lookup({
from:'fzxk_learn_process',
localField: '_id',
foreignField: 'course_id',
as: 'peocessList',
})
.end()
.catch(err => console.error(err))
多表查询云函数--未知参数数据
const cloud = require('wx-server-sdk') // 引用SDK
cloud.init({ // 数据库初始化
env: cloud.DYNAMIC_CURRENT_ENV
})
var db = cloud.database()
var $ = db.command.aggregate
const _ = db.command
// event --- 参数
// const data = {
// callFunctionType:'cms_List',
// column:'2d44d6c2611b1cd40593615f725359d3',
// };
// 云函数入口函数
exports.main = async (event, context) => {
const id = event.id;
const user_id = event.user_id
const column = event.column
switch (event.callFunctionType) {
case 'cms_List':
try {
//articleTest 查询的表名
return await db.collection('articleTest').aggregate()
.lookup(
{
from: "article_operator", // 子表 和主表匹配
let: {
article_id: '$_id' // article_id (取名称) -- $_id (articleTest表 的参数_id)
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$article_id', '$$article_id']), // $article_id (article_operator表的参数名称) --- $$article_id (articleTest表 let 声明的变量 _id)
$.eq(['$type', 1]) // $type (article_operator表的参数名称)
])))
.done(),
as: 'fabulous',
}
)
.lookup({
from: "article_operator", // 子表 和主表匹配
let: {
article_id: '$_id' // 必须声明主表参数,如果不声明无法用
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$article_id', '$$article_id']), // $article_id (article_operator表的参数名称) --- $$article_id (articleTest表 let 声明的变量 _id)
$.eq(['$type', 2])// $type (article_operator表的参数名称)
])))
.done(),
as: 'review', // 取参数名称
})
.match({
column:column //(主表的参数名称) column (column参数 === 'id')
})
.end() // 标志聚合操作定义完成,发起实际聚合操作
.catch(err=>console.log(err))
} catch (error) {
}
}
}
多表查询云函数--已知参数数据
const cloud = require('wx-server-sdk') // 引用SDK
cloud.init({ // 数据库初始化
env: cloud.DYNAMIC_CURRENT_ENV
})
var db = cloud.database()
var $ = db.command.aggregate
const _ = db.command
// event --- 参数
// const openId = wx.getStorageSync('openId')
// const data = {
// callFunctionType:'cms_Info',
// match:{
// _id:id,
// },
// database:'articleTest',
// user_id:openId,
// id:id,
// };
exports.main = async (event, context) => {
// const wxContext = cloud.getWXContext()
console.log(event.id)
const id = event.id;
const user_id = event.user_id
const column = event.column
switch (event.callFunctionType) {
case 'cms_Info':
try {
return db.collection(event.database).aggregate() // event.database -- 查询的表名
.lookup(
{
from: "article_operator", // 子表
let: {},
pipeline: $.pipeline()
.match({
article_id:id, //article_id -- (article_operator 名称)
type: 2
}).done(),
as: 'fabulous', // 参数名 会覆盖
}
)
.lookup({
from: "article_operator", // 子表
let: {
},
pipeline: $.pipeline()
.match({
article_id:id, //article_id -- (article_operator 名称)
type: 1
}).done(),
as: 'review',
})
.lookup({
from: "article_operator",
let: {
user_id:'$user_id',
},
pipeline: $.pipeline()
.match({
article_id:id,
type: 2,
user_id:user_id,
}).done(),
as: 'isHit',
})
.match(event.match) //column (column参数 === 'id')
.end() // 标志聚合操作定义完成,发起实际聚合操作
.catch(err=>console.log(err))
} catch (error) {
}
}
}