小程序雲開發實現模糊匹配,模糊搜索,小程序雲開發正則匹配數據庫

代碼說明:模糊查詢數據庫集合(birthday),模糊匹配查詢生日是否爲今天

用戶的生日日期格式爲 1994-07-06 , 當前日期格式爲 07-06 , 匹配數據庫的月和日,數據庫集合如圖:

雲函數代碼:

// 雲函數入口文件
const cloud = require('wx-server-sdk')
const Core = require('@alicloud/pop-core');

cloud.init()
const db = cloud.database()

function getDate(){
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  if (month < 10) {
      month = "0" + month;
  }
  if (day < 10) {
      day = "0" + day;
  }
  var nowDate = month + "-" + day;
  return nowDate;
}

// 雲函數入口函數
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  const _ = db.command
  const birthdayArr = await db.collection('birthday').where({
    _date: db.RegExp({
      regexp: getDate(),
      options: 'i',
    })
    }).get().then(res => {
      return res.data;
    })
    
  return  birthdayArr;
  
}

返回結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章