数据库集合1:user
[{"userId":"zqiang","age":20},
{"userId":"lling","age":27},
{"userId":"wcjing","age":19},
{"userId":"wxia","age":21},
{"userId":"zying","age":18}]
集合2:projects
[{"name":"zqiang","subject":"体育","create-date":"Sat Aug 01 2020 13:45:26 GMT+0800 (中国标准时间)","province":"北京市","city":"北京市","district":"海淀区"},
{"name":"lling","subject":"英语","create-date":"Sat Jul 18 2020 16:45:39 GMT+0800 (中国标准时间)","province":"广东省","city":"广州市","district":"天河区"},
{"name":"wcjing","subject":"数学","create-date":"Fri Aug 09 2019 16:50:52 GMT+0800 (中国标准时间)","province":"辽宁省","city":"沈阳市","district":"铁西区"},
{"name":"lling","subject":"美术","create-date":"Sun Aug 02 2020 16:56:41 GMT+0800 (中国标准时间)","province":"上海市","city":"上海市","district":"徐汇区"},
{"name":"wcjing","subject":"英语","create-date":"Tue Jun 23 2020 17:02:03 GMT+0800 (中国标准时间)","province":"江西省","city":"南昌市","district":"东湖区"},
{"name":"zqiang","subject":"英语","create-date":"Thu Jun 18 2020 18:04:06 GMT+0800 (中国标准时间)","province":"福建省","city":"福州市","district":"台江区"},
{"name":"wxia","subject":"英语","create-date":"Sun Jun 07 2020 22:08:08 GMT+0800 (中国标准时间)","province":"广东省","city":"深圳市","district":"南山区"},
{"name":"zying","subject":"英语","create-date":"Tue Jun 23 2020 13:09:03 GMT+0800 (中国标准时间)","province":"北京市","city":"北京市","district":"丰台区"}]
小程序端通过普通picker和region picker传来几个筛选参数:age,subject,date,region(包含region[0],region[1],region[2])
要求:通过这4个参数查询数据库中的数据,age,subject,date,region传过来的参数等于0,或者region[0],region[1],region[2]三个字符串为“全部”时跳过该筛选条件。若参数有效,则分别筛选>age,=subject,
我的程序:
const db = cloud.database()
const _ = db.command
const projects = db.collection("projects")
const $ = _.aggregate
async function getprojectslist (age,date,subject,region){
//根据date参数判断create-date是全部?1天内?3天内?7天内?
let mseconds = 0
if(date == 0){
mseconds = 0
}else if(date == 1){
mseconds = -86400000
}else if(date == 2){
mseconds = -259200000
}else if(date == 3){
mseconds = -604800000
}
let daysel = db.serverDate({offset:mseconds})
//设置查询条件
projectsMatch = []
//查询条件为空,返回所有数据
if((age== 0 && date == 0 && subject == 0 && region == 0) || (age== 0 && date == 0 && subject == 0 && region[0] == "全部" && region[1] == "全部" && region[2] == "全部")){
const projectList = projects.get().then(res =>{
return res
})
return projectList
}else{//判断至少存在一个筛选条件,把有用的筛选条件放入数组
if(date != 0){
projectsMatch.push($.gte(['$date', daysel]))
}
if(trade != 0){
projectsMatch.push($.eq(['$subject', subject]))
}
if(region != 0){
projectsMatch.push($.eq(['$province', region[0]]))
}
if(region != 0 && region[1] != "全部"){
projectsMatch.push($.eq(['$city', region[1]]))
}
if(region != 0 && region[2] != "全部"){
projectsMatch.push($.eq(['$district', region[2]]))
}
const projectList = db.collection('projects').aggregate()
.lookup({
from: "user",
localField: "name",
foreignField: "userId",
as: "uid"
})
.replaceRoot({//把user集合中的age根据userId和name匹配联表查询放入projects集合中
newRoot: $.mergeObjects([ $.arrayElemAt(['$uid', 0]), '$$ROOT' ])
})
.project({
uid: 0
})
.match(_.expr($.and([
// $.gte(['$age',Number(age)]),//挨个调试过,单独的功能正常,但多个条件组合就只返回最后一个筛选条件的结果
$.gte(['$date',daysel]),//单独反复调试,一直无效
// $.gte(['$subject,subject]),//没有调试过
// $.eq(['$province',region[0]]),//挨个调试过,单独的功能正常,但多个条件组合就只返回最后一个筛选条件的结果
// $.eq(['$city',region[1]]),//挨个调试过,单独的功能正常,但多个条件组合就只返回最后一个筛选条件的结果
// $.eq(['$district',region[2]])//挨个调试过,单独的功能正常,但多个条件组合就只返回最后一个筛选条件的结果
// projectsMatch//单独调试过,把这个条件数组放在里面无效
])))
.end()
.then(res => {
return res
})
return projectList
}
}
请大神帮忙看看问题出在哪儿,感激不尽!!!!!
const db = cloud.database() const _ = db.command const projects = db.collection("projects") const $ = _.aggregate async function getprojectslist (age,date,subject, _region){ let mseconds = 0, daysel, query, region = Array.isArray(_region) ? _region: [], defMatch = { name:$.neq('') },//默认条件 projectsMatch = [], timeArr = {0:0, 1:-86400000, 2:-259200000, 3:-604800000}, flag1 = !age && !date && !subject && !region.length, flag2 = !age && !date && !subject && region.join('') == '全部全部全部', projectList = [] mseconds = date in timeArr ? timeArr[date] : 0 daysel = db.serverDate({offset:mseconds}) query = projects.aggregate() query = query.match(defMatch) if(flag1 || flag2){ projectList = await query.end() console.log('projectList 1') }else{ //判断至少存在一个筛选条件,把有用的筛选条件放入数组 date && projectsMatch.push($.gte(['$date', daysel])) age && projectsMatch.push($.gt(['$age', age])) subject && projectsMatch.push($.eq(['$subject', subject])) region[0] && projectsMatch.push($.eq(['$province', region[0]])) region[1] && projectsMatch.push($.eq(['$city', region[1]])) region[2] && projectsMatch.push($.eq(['$district', region[2]])) projectList = await query.lookup({ from: "user", localField: "name", foreignField: "userId", as: "uid" }) .replaceRoot({ //把user集合中的age根据userId和name匹配联表查询放入projects集合中 newRoot: $.mergeObjects([ $.arrayElemAt(['$uid', 0]), '$$ROOT' ]) }) .match(_.expr($.and([...projectsMatch]))) .project({ uid: 0 }) .end() console.log('projectList 2=>', projectsMatch) } return projectList }
若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人