微信小程序—云开发模糊搜索数据库制作搜索功能

最近微信小程序需要做个搜索的功能,但是微信小程序的云开发中的数据库的查询语句中没有模糊查询这个功能【小声BB:真垃圾】,神奇的是有正则表达式这个功能,正则表达式提供以下方法

 	wx.cloud.init()
    var that=this
    const db = wx.cloud.database()
    const _ = db.command
    db.collection('passage_list')
    .where(_.or([{
    
    
      title: db.RegExp({
    
    
        regexp: '.*' + '关键字',
        options: 'i',
      })
     }
    ]))
    .limit(20)
    .get({
    
    
      success:function(res){
    
    
        console.log(res.data);
      }
    })

由于我这里只需要从文章标题这里搜索数据库,如果需要多字段搜索的话可以这样使用多字段

 .where(_.or([{
    
    
      title: db.RegExp({
    
    
        regexp: '.*' + '关键字',
        options: 'i',
      }),
      passage: db.RegExp({
    
    
        regexp: '.*' + '关键字',
        options: 'i',
      })
     }
 ]))

猜你喜欢

转载自blog.csdn.net/AcStudio/article/details/107741982
今日推荐