我在 Sequelize 中有以下查询
let regMonth = [];
let month = 1;
while (month < 13) {
const currMonth = await registrations.findAndCountAll({
attributes: ['id'],
where: {
createdAt: {
[Op.gte]: moment("0101", "MMDD").subtract((30 * month)-365, 'days').toDate()
}
}
})
regMonth.push(currMonth);
month++;
}
它应该创建一个数组,其中包含各个月份创建的数据库记录的数量。它在某种程度上有效,但生成的数组如下所示:
[3,3,0,0,0,0,0,0,0,0,0,0]
但它应该返回
[0,3,0,0,0,0,0,0,0,0,0,0]
因为我只在 2 月份添加了 3
条记录,其他月份没有添加。
我该如何解决这个问题,或者是否有其他更有效的方法可以做到这一点,可能使当前年份变得无关紧要。
感谢您的帮助。
最佳答案
createdAt: {
[Op.gte]: moment("0101", "MMDD").add(month-1, 'months').toDate(),
[Op.lt]: moment("0101", "MMDD").add(month, 'months').toDate(),
}
请记住数据库中的created_at值与服务的当前时区之间的时区差异(如果有)。
关于javascript - 如何获取每月的数据库记录数量 Sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60396082/