我为此苦苦思索了几个小时——试图在 and
块内进行日期比较。这两个部分让我困惑了一段时间——日期比较部分和 and
语法。
这是不起作用的语法 - 然后我会用有效的代码发布答案。
注意: user_presence_time_of_last_update
是 time stamp with timezone
类型的字段。
不起作用
const op = connectors.Sequelize.Op;
const timeTwoHoursAgo = new Date()
timeTwoHoursAgo.setMinutes(timeTwoHoursAgo.getMinutes() - 120)
var usersWhoWentOffline = connectors.epUserData.findAll({
where: {
$and:{
user_presence_time_of_last_update: {$lt: new Date(new Date() - 5 * one_hour)},
$or:[
{user_presence: USER_PRESENCE_IDLE},
{user_presence: USER_PRESENCE_ONLINE},
],
}
}
}).then((res) => res.map((item) => item.dataValues));
最佳答案
这是在 this SO post 的大力协助下确实有效的代码:
确实有效
const op = connectors.Sequelize.Op;
const timeTwoHoursAgo = new Date()
timeTwoHoursAgo.setMinutes(timeTwoHoursAgo.getMinutes() - 120)
var usersWhoWentOffline = connectors.epUserData.findAll({
where: {
[op.and]: {
user_presence_time_of_last_update: {
[op.lt]: timeTwoHoursAgo
},
[op.or]: [
{user_presence: USER_PRESENCE_IDLE},
{user_presence: USER_PRESENCE_ONLINE},
]
}
}
})
关于postgresql - Sequelize : Date Comparison Inside of an AND Block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62126817/