1. 单字段模糊查询

 const res = await db.collection("table").limit(query.limit = 10).where({
        name:{
          $regx:'.*'+query.name,
          $options:'i'
        }
      }).skip(((query.page = 1) - 1) * (query.limit = 10)).field({}).orderBy("createTime", "desc").get();

查询集合为table里面的name字段,like你传入的所有值

$options:'i'是代表传入的值不区分大小写

 

2. 多字段

let key = "编程小石头";
    console.log("查询的内容", key)
    const db = wx.cloud.database();
    const _ = db.command
    db.collection('qcl').where(_.or([{
        name: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        address: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      }
    ])).get({
      success: res => {
        console.log(res)
      },
      fail: err => {
        console.log(err)
      }
    })

key就是我们要搜索的关键字。主要是用到了数据库查询的where,or,get方法。

posted on 2022-03-15 11:18  12day  阅读(206)  评论(0)    收藏  举报