数据表有以下字段 :
_id:****,
_openid:(string)
num1:(number),
num2:(number),
Date:(date)
user:(object)
…(省略)
一.有以下需要同时满足的条件:(条件a,b,c)
条件a:_openid === “abc”,
条件b:num1 !== -2
条件c:num1 !== -1
三个条件需同时满足,虽然num1可以直接<0(这里不是重点)
直接写在函数里面:geoNear({
query:{
_.eq({_openid:“abc”})_.neq({num1:-2})_.neq({num1:-1})//我自己没试过,应该可以
//或:\_.eq({\_openid:"abc"}).and(\_.neq({num1:-2})).and(\_.neq({num1:-1}))
//或:\_.and(\_.eq({<span style="font-size: 16px;">\_openid:"abc"</span>}),<span style="font-size: 16px;">\_.neq({num1:-2}),\_.neq({num1:-1})</span>)
}
})
我的问题:不知道query在函数外面定义的时候怎么在for循环里面定义
/*****************************************************大体上 我的函数像下面这样写***********************************************/
var TiaoJian={};
TiaoJian._openid=_.eq("abc")//这样写可以
for(let i=0;i<X;i++){
if(其它条件A===true){
TiaoJian.num1=neq(-2)
}
if(其它条件B===true){
TiaoJian.num1=neq(-1)
}
}
let a= await ABC(TiaoJian)//调用ABC()获取数据 (函数外面有async,省了,占位置)
console.log(a)//会将所有信息给返回,连数据表_openid字段不为"abc"都给出来........
//以上for里面的写法写进去后,db.collection().aggregate().geoNear({query:TiaoJian}).end()会将所有信息给返回,连_openid不为"abc"都给出来........
//求for(let i=0;i<X;i++)里面的TiaoJian的写法!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//求for(let i=0;i<X;i++)里面的TiaoJian的写法!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//求for(let i=0;i<X;i++)里面的TiaoJian的写法!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
fucntion ABC(TiaoJian){
return new Promise((resolve, reject) =>db.collection().aggregate()
.geoNear({
near: db.Geo.Point(X,Y)
spherical: true,
distanceField: 'Distance',
distanceMultiplier: 0.001,
query:TiaoJian//跪求:条件需要在函数外面提前定义好!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
})
.match({coordinate: _.exists(true)})
.project(returnField)
.limit(20)
.sort({num1: -1, Distance: 1, Date: -1 })//排系方式先:num1丛小到大再Distance从大到小再date从小到大
.end({
success(res){resolve(res.list)},
fail(err){reject(err)}
})
})
文档https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.and.html最后一段
调用风格
方法接收两种传参方式,一是传入一个数组参数,二是传入多个参数,效果一样。
// 传入数组
function and(expressions: Expression[]): Command
// 传入多参数
function and(...expressions: Expression[]): Command
不理解,被学js,求指教,谢谢了!!!