我想先查询用户的openid然后再跨字段进行 "或" 操作,所以我用了.where().where()的方式,但是感觉很不规范,请问如何才能优化只有一个where()呢?
let w = [], id = {} if (keyword.trim() != '') { w = [{ ar: db.RegExp({ regexp: keyword, options: 'i' }) }, { name: db.RegExp({ regexp: keyword, options: 'i' }) } ] } id = { _openid: wxContext.OPENID } //先获取openid 然后再进行条件搜索,条件搜索数组不能为空,所以进行了一个判断 if (w.length != 0) { ctx.body = await cloud.database().collection('collectMusiclist').where(id).where(_.or(w)).skip(event.start).limit(event.count) .orderBy('createTime', 'desc') .get().then(res => { return res }) } else { ctx.body = await cloud.database().collection('collectMusiclist').where({ _openid: wxContext.OPENID }).skip(event.start).limit(event.count) .orderBy('createTime', 'desc') .get().then(res => { return res }) } |
新手一枚,想请各位大佬解答一下

const _ = db.commandlet re =newRegExp(event.word,"i")let data = await db.collection('user').where(_.or([{openId: re}, {'userInfo.nickName': re}])).get()db.RegExp 不能用在command里面,只能单独使用,所以你这里把w的两个条件拆开来,数据库查两次,这样都写在where里就可以了吧。
第一次查询:
where({_openid: wContent.OPENID,ar: db.RegExp({regexp: keyword,options:'i'})})第二次查询:
where({_openid: wContent.OPENID,name: db.RegExp({regexp: keyword,options:'i'})})再自己把两个查询得到的数组拼接起来。
可以看下我之前的帖子参考:
https://developers.weixin.qq.com/community/develop/doc/000ca2cfee0c387c9069e27c95b400