微信小程序云开发按月份查找问题?
发布于 5 年前 作者 yuanjing 7043 次浏览 来自 问答

目前使用微信云开发,数据库也是云数据库,现在需要从数据库 生日表中 查询本月过生日的人员信息,之前用SQL时一般用select from student where month(birthday)=month(getdate())   从student表中,直接将birthday字段格式化成month进行比对,小程序中试了不能用。请问有没有类似的方法??

数据库生日存的是date类型


有没人大神提个思路,我自己去查资料就行,自己想了好久,试了好多方法,百度也查不到,实在不行我只能在数据库里面加一个月份的字段,专门存月份,但是感觉好LOW,即然存了日期格式就应该可以取才对呀。

2 回复
//调试通过:新生成一个字段birthmonth, 再筛选(match)
var lnmonth = 9   // calend month
var db1 = wx.cloud.database()
const $ = db1.command.aggregate
db1.collection('col1').aggregate()
  .project({ studname: true, birthday: true, birthmonth: $.month('$birthday') })
  .match({birthmonth: lnmonth})
  .end()
  .then(res => {
    console.log(res)
  }, err => {
    console.log('error1: ', err)
  })
回到顶部