1. 程式人生 > 其它 >雲開發cloudbase怎麼實現單欄位、多欄位模糊查詢

雲開發cloudbase怎麼實現單欄位、多欄位模糊查詢

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方法。